missing concrete implementation of state.build

南楼画角 提交于 2021-01-01 06:43:10

问题


I am getting this error in Dart: "Missing concrete implementation of "state.build""

The first method is the following:

class MyHomePage extends StatefulWidget {
  // String titleInput;
  // String amountInput;
  @override
  MyHomePageState createState() => MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  final List<Transaction> _userTransactions = [
    // Transaction(
    //   id: "t1",
    //   title: "New Shoes",
    //   amount: 69.99,
    //   date: DateTime.now(),
    // ),
    // Transaction(
    //   id: "t2",
    //   title: "Weekly Groceries",
    //   amount: 16.53,
    //   date: DateTime.now(),
    // ),
  ];

Does anyone knows what this error means and how to solve it?

Thank you.


回答1:


You need to add a build method to the State of your widget, this method describes the part of the user interface represented by your widget, e.g., (add this to the MyHomePageState)

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child:
            Container(
              height: 200,
              width: 100,
              color:  Colors.yellow,
        ),
      ),
    );
  }



回答2:


All the Stateful widgets and Stateless widgets should have build method.

@override
Widget build(BuildContext context) {
    return Container(
       ...
    );
}

If you want to use it without build do not extend the class with State, use it like

class YourClassName {
}



回答3:


Goto the definitation of State<T> class and see what are the abstract methods. You will find build() method as the only abstract method i.e. a method without body. So when yu are inheriting from State<MyHomePage>, you must override the build() and give a body; basically you will create your Widgets inside the build() method.

So to fix the error add the below code to your class:

class MyHomePageState extends State<MyHomePage> {
   @override
   Widget build(BuildContext context) {
      return Scaffold( // Your Widget
         ...
      );
   }
}



回答4:


Just check

@override
Widget build( Buildcontext context)
{ 
return Container();
}

Check spelling of build

reason : As u extend your class with stateless or statefull widget , U use overiding method to over ride the pre defined method which is already written in parent class which is state less/full class



来源:https://stackoverflow.com/questions/63246564/missing-concrete-implementation-of-state-build

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!