【问题标题】:Error Missing concrete implementation of 'StatefulWidget.createState'. Try implementing the missing method, or make the class abstract错误缺少“StatefulWidget.createState”的具体实现。尝试实现缺少的方法,或使类抽象
【发布时间】:2021-11-09 10:39:43
【问题描述】:
import 'package:flutter/material.dart';

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

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage("asset/image/Bestone.jpg")))));
  }
}

无论我使用什么,我在尝试运行或编码时都会遇到问题。我收到此错误。

缺少“StatefulWidget.createState”的具体实现。尝试实现缺少的方法,或者将类抽象为 MyApp ()

【问题讨论】:

    标签: flutter dart stateful statefulwidget


    【解决方案1】:

    StatefulWidget 需要覆盖方法createState 所以你必须覆盖那个方法。删除 build 方法并在 MyApp 中指定以下行

    @override
    _MyHomePageState createState() => _MyHomePageState();
    

    带有适当 StateFulWidget 的完整示例

    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      final String title;
    
      const MyHomePage({
        Key? key,
        required this.title,
      }) : super(key: key);
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 2020-06-11
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-01
      相关资源
      最近更新 更多