Home > Net >  The values in a const list literal must be constants, I am getting this error while adding an Image
The values in a const list literal must be constants, I am getting this error while adding an Image

Time:01-04

Why am I unable to add an Image here? I am getting this error while adding an Image to Column Widget in Flutter. When I am removing the new keyword, I am getting another error. Please check into this issue . I have made a folder named assets>images>girl-icon.jpg . Also, I have enabled assets from pubspec.yaml file.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'My First Flutter App',
        theme: ThemeData(
          scaffoldBackgroundColor: Colors.white,
        ),
        home: const WelcomeScreen());
  }
}

class WelcomeScreen extends StatelessWidget {
  const WelcomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('First Page'),
        backgroundColor: Colors.amber,
      ),
      body:Center(

      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: const <Widget> [

          Text("Login",
              style: TextStyle(
              fontSize: 30,
              fontWeight: FontWeight.bold,
              )),
          new Image.asset("assets/images/girl-icon.jpg"),
        ],
        ),
        heightFactor: 30,

      ),
      );
    }}

See screenshot of the problem here

CodePudding user response:

const requires a hard coded constant value and you have called MyApp as constant while it's dynamically being build that is why it's throwing an error. Remove const before MyApp to solve this issue :

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

and also from here :

children: <Widget> [

          Text("Login",
              style: TextStyle(
              fontSize: 30,
              fontWeight: FontWeight.bold,
              )),
          new Image.asset("assets/images/girl-icon.jpg"),
        ]

CodePudding user response:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'My First Flutter App',
        theme: ThemeData(
          scaffoldBackgroundColor: Colors.white,
        ),
        home: const WelcomeScreen());
  }
}

class WelcomeScreen extends StatelessWidget {
  const WelcomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('First Page'),
        backgroundColor: Colors.amber,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("Login",
                style: TextStyle(
                  fontSize: 30,
                  fontWeight: FontWeight.bold,
                )),
            Image.asset("assets/images/girl-icon.jpg"),
          ],
        ),
        heightFactor: 30,
      ),
    );
  }
}

my way is just to remove the

const <Widget> 
  • Related