0

enter image description here

I am new to Android Development. I created a flutter project and just returned a Text Widget using a stateless class but I am getting this error. I tried reading about it on the blogs regarding this error. I think its related to calling an instance of a stateless widget in the same class itself but I am not sure.

stack overflow post

Here's my code:


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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('Hello'),
    );
  }
}

Getting this O/P: enter image description here

What to do ?

Vidhu Verma
  • 223
  • 1
  • 11

1 Answers1

2

You must use MaterialApp Widget in the beginning. If you do this, the problem will be solved. But I recommend you to wrap the Text Widget with Scaffold too.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Text('Hello'),
    );
  }
}
Hazar Belge
  • 1,009
  • 4
  • 20
  • Yes using MaterialApp worked but what is the reason for the error that I am getting in my code ? Do we need to use Material App everytime ? – Vidhu Verma Aug 28 '21 at 18:40
  • 1
    You're running an app. Therefore, you have to use it. It creates and bind everything that needed for run an app. After that you'll see another warning (Text Widget isn't work properly). Because of the extinction of Scaffold Widget. You can find more info for Material App [here](https://api.flutter.dev/flutter/material/MaterialApp-class.html) and for Scaffold [here](https://api.flutter.dev/flutter/material/Scaffold-class.html) – Hazar Belge Aug 28 '21 at 19:33