错误缺少“StatefulWidget.createState”的具体实现。尝试实现缺少的方法,或使类抽象
Posted
技术标签:
【中文标题】错误缺少“StatefulWidget.createState”的具体实现。尝试实现缺少的方法,或使类抽象【英文标题】:Error Missing concrete implementation of 'StatefulWidget.createState'. Try implementing the missing method, or make the class abstract 【发布时间】: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 ()
【问题讨论】:
【参考方案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),
),
);
【讨论】:
以上是关于错误缺少“StatefulWidget.createState”的具体实现。尝试实现缺少的方法,或使类抽象的主要内容,如果未能解决你的问题,请参考以下文章
SQL 错误:ORA-00907:缺少右括号 00907。00000 -“缺少右括号”
React componentDidMount“解析错误:缺少分号”
Cloudinary 错误:“错误”:“消息”:“缺少必需的参数 - 时间戳”