0

new to Flutter so may be missing something that others find 'obvious'. If so - apologies.

I have just started learning Dart/Flutter on Udemy. I've come across a head-scratcher:

When I run one lot of code in an emulator it runs fine, when I run a very similar piece of code it builds without problems but displays absolutely nothing.

I'm writing the code in VSCode and using an emulator from Android Studio.

The code which works is:

// ignore_for_file:prefer_const_constructors

import 'package:flutter/material.dart';

void main() {
  runApp(MyDashater());
}

class MyDashater extends StatelessWidget {
  const MyDashater({super.key});
  // Declare any local variables:

  final int colourBlack = 0xff000000,
      colourGreen = 0xff119b11,
      colourYellow = 0xffffff00;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Turn off the debug mode banner:
      debugShowCheckedModeBanner: false,

      // Main code block - put everything into the Scaffold:
      home: Scaffold(
        backgroundColor: Color(colourGreen),
        appBar: AppBar(
          backgroundColor: Color(colourBlack),
          centerTitle: true,
          titleTextStyle: TextStyle(color: Color(colourYellow)),
          title: Text("My Dashatar App"),
        ),
        body: Center(child: Image.asset('images/mjb_dashatar1.png')),
      ),
    );
  }
}

On the emulator this displays: App displaying correctly on emulator

The code which works fine in Windows/any web browser, but not in the emulator is:

// ignore_for_file:prefer_const_constructors

import 'package:flutter/material.dart';

void main() {
  runApp(MyDashater());
}

class MyDashater extends StatelessWidget {
  const MyDashater({super.key});
  // Declare any local variables:

  final int colourBlack = 0xff000000,
      colourGreen = 0xff119b11,
      colourYellow = 0xffffff00;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Turn off the debug mode banner:
      debugShowCheckedModeBanner: false,

      // Main code block - put everything into the Scaffold:
      home: Scaffold(
        backgroundColor: Color(colourGreen),
        appBar: AppBar(
          backgroundColor: Color(colourBlack),
          centerTitle: true,
          titleTextStyle: TextStyle(color: Color(colourYellow)),
          title: Text("My Dashatar App"),
        ),
        body: Center(child: Image.asset('images/mjb_dashatar1.png')),
      ),
    );
  }
}

On the emulator this displays: Emulator blank screen

I was expecting the app to launch similarly to how it does when using Windows or a web browser: Working Win 11 version of App

I'd really appreciate any possible pointers. Thank you.

3
  • 1
    when running on emulator try to check debug console if there is any message like exception ,... and if there is include it on your question Commented Oct 11 at 15:26
  • 1
    can you make sure the image path has been added in pubspec.yaml, next you can try flutter clean and rerun the app Commented Oct 12 at 3:27
  • 1
    your two code blocks are identical. But if I had to guess it's maybe because of the image size. Android can run out of memory when using too large images Commented Oct 13 at 11:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.