'Why am i getting the default flutter example code every time i run my new flutter code on my phone?

I am using my android phone to run my flutter code. The problem is when i run any code(i.e. different from the auto generated flutter code that is created when i create a flutter project)it always shows me the output of the auto generated flutter code. I have to reload it after each run to get the output of my new code. Plus, after i detached my phone from my PC the app i get on my phone is still the auto generated one. I have used both android studio and VS code but the problem still persists. I have tried to create new project but still no luck.

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)),
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

When i run the this code, it compile and run normally without error or exceptions. It goes like this:

  • Launching lib\main.dart on SM G950F in debug mode...
  • √ Built build\app\outputs\apk\debug\app-debug.apk.
  • D/FlutterActivity( 9631): Using the launch theme as normal theme. D/FlutterActivityAndFragmentDelegate( 9631): Setting up FlutterEngine. D/FlutterActivityAndFragmentDelegate( 9631): No preferred FlutterEngine was provided. Creating a new FlutterEngine for this FlutterFragment. D/FlutterActivityAndFragmentDelegate( 9631): Attaching FlutterEngine to the Activity that owns this Fragment. D/FlutterView( 9631): Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@133f78d D/FlutterActivityAndFragmentDelegate( 9631): Executing Dart entrypoint: main, and sending initial route: / D/ViewRootImpl@bcf80b7MainActivity: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1080, 2220) ci=Rect(0, 63 - 0, 0) vi=Rect(0, 63 - 0, 0) or=1 D/InputTransport( 9631): Input channel constructed: fd=99 D/InputMethodManager( 9631): prepareNavigationBarInfo() DecorView@13938bf[MainActivity] D/InputMethodManager( 9631): getNavigationBarColor() -855310 V/InputMethodManager( 9631): Starting input: tba=com.keyetwedet.keyet_wedet ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false D/InputMethodManager( 9631): startInputInner - Id : 0 E/AccessibilityBridge( 9631): VirtualView node must not be the root node.

And when it finally launches the code on my phone, no matter what code i wrote, it always show the flutter sample code output like this:

This is what i always get on the first run

So, in order to get my phone showing my code i have to "hot restart" it. Only then i will get the output of my code.

This is my code's output which comes after i hot restarted it

But after i stopped the debugging and detached my phone from my PC, the app i get on my phone is still the output of the flutter sample code.

In summary my questions are the following:

  1. Why isn't it showing my code's output on the first run.
  2. Why isn't it saving my code on my device so that when i open the app on my phone it could show me the output of my code.


Solution 1:[1]

I tried many things and found out that if I run flutter clean on the terminal before going into debugging it solves the problem!

Solution 2:[2]

I'm also facing the same issue. But I found what went wrong.

  • Problem:

I didn't save the code after making changes in default example.

  • Solution:

Press CTRL+s to save each time any change is made in code.

  • Preferred solution:

Go to menu in Visual Studio Code, File -> Click 'Auto save'. So that you don't have to save changes each time, it will be auto-saved.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 abene42
Solution 2 Kousalya