首页 > 解决方案 > 为什么每次在手机上运行新的颤振代码时都会获得默认的颤振示例代码?

问题描述

我正在使用我的安卓手机来运行我的颤振代码。问题是当我运行任何代码时(即与我创建颤振项目时创建的自动生成颤振代码不同),它总是向我显示自动生成颤振代码的输出。每次运行后我都必须重新加载它才能获得新代码的输出。另外,在我将手机与 PC 分离后,手机上的应用程序仍然是自动生成的应用程序。我已经使用了android studio和VS code,但问题仍然存在。我试图创建新项目,但仍然没有运气。

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';


void main() => runApp(new MaterialApp(home: new KeyetWedetApp ())); //The runApp() function takes the given Widget and makes it the root of the widget tree.

class KeyetWedetApp extends StatefulWidget {
  @override
  _State createState() => new _State();
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.red,
      ),
      home: new KeyetWedetApp(),
      darkTheme: new ThemeData(accentColor: Colors.deepPurpleAccent),
    );
  }
}

class _State extends State<KeyetWedetApp> {
  static List _pageSelectorList = <Widget>[
    Image.asset('assets/images/balloon.jpeg'),
    Image.asset('assets/images/beach.jpeg'),
    Image.asset('assets/images/camp.jpeg'),
    Image.asset('assets/images/hello.png'),
    Image.asset('assets/images/mountain.jpeg')
    //TODO Insert our own images which describe what our application will do step by step.
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: new Container(
          child: new DefaultTabController(
            length: _pageSelectorList.length,
            child: Builder(
              builder: (BuildContext context) => Padding(
                padding: EdgeInsets.fromLTRB(10.0, 30.0, 10.0, 10.0),
                child: new Column(
                  children: <Widget>[
                    new TabPageSelector(selectedColor: Colors.blue),
                    new Expanded(
                        child: IconTheme(
                      data: IconThemeData(
                        size: 200.0,
                        color: Theme.of(context).accentColor,
                      ),
                      child: TabBarView(
                        children: _pageSelectorList,
                      ),
                    )),
                    new FlatButton(
                      onPressed: () {
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => HomePage()));
                      },
                      child: new Text(
                        'Let\'s Get Started',
                        style: new TextStyle(fontSize: 20, color: Color(0xFF757575)),
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

当我运行这段代码时,它编译并正常运行,没有错误或异常。它是这样的:

当它最终在我的手机上启动代码时,无论我写什么代码,它总是显示如下所示的颤振示例代码输出:

这就是我第一次跑步时总是得到的

所以,为了让我的手机显示我的代码,我必须“热重启”它。只有这样我才能得到我的代码的输出。

这是我热重启后的代码输出

但是在我停止调试并将手机与 PC 分离后,我手机上的应用程序仍然是颤振示例代码的输出。

总之,我的问题如下:

  1. 为什么它在第一次运行时不显示我的代码输出。
  2. 为什么不将我的代码保存在我的设备上,以便当我在手机上打开应用程序时它可以显示我的代码输出。

标签: androidflutter

解决方案


我尝试了很多东西,发现如果我flutter clean在调试之前在终端上运行它可以解决问题!


推荐阅读