Flutter 顯示錯誤 Non-nullable instance field '_areas' must be initialized。也許這是因為沒有在串列區域中定義 null 在定義 null Like List 時會發生什么?_地區;它在索引上顯示錯誤
錯誤:應初始化欄位“_areas”,因為其型別“串列”不允許為空。
錯誤行:列出_areas;
這是我的代碼請幫幫我
import 'package:flutter/material.dart';
import 'dart:math';
void main() {
runApp(new MaterialApp(
home: new MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_State createState() => new _State();
}
class Area {
int index;
String name;
Color color;
Area({this.index: -1, this.name: 'Area', this.color: Colors.lightBlueAccent});
}
class _State extends State<MyApp> {
int _location = 0;
List<Area> _areas;
@override
void initState() {
//_areas = new List<Area>();
List _areas = [];
for(int i = 0; i<16; i ){
_areas.add(new Area(index: i, name: 'Area ${i}'));
}
var rng = new Random();
_location = rng.nextInt(_areas.length);
}
Widget _generate(int index){
return new GridTile(
child: new Container(
padding: new EdgeInsets.all(5.0),
child: new RaisedButton(
onPressed: () => _onPressed,
color: _areas[index].color,
child: new Text(_areas[index].name, textAlign: TextAlign.center,),
),
)
);
}
void _onPressed(int index){
setState((){
if(index == _location) {
_areas[index].color = Colors.green;
//You won
} else {
_areas[index].color = Colors.red;
}
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Grid'),
backgroundColor: Colors.deepPurpleAccent,
),
body: new Container(
padding: new EdgeInsets.all(32.0),
child: new Center(
child: new GridView.count(
crossAxisCount: 4,
children: new List<Widget>.generate(16, _generate),
)
)
),
);
}
}
我將它從(List _areas;更改為List?_areas;)但它再次顯示錯誤
uj5u.com熱心網友回復:
洗掉方法中的List
宣告initState
,這應該可以解決它。
但請記住將可為空的運算子添加到類屬性中:
List<Area>? _areas;
不過,一般來說,最好不要使用可空串列。相反,您可以使用一個空串列來啟動該屬性,然后再為其添加值。
List<Area> _areas = [];
@override
void initState() {
for(int i = 0; i < 16; i ) {
_areas.add(Area(index: i, name: 'Area ${i}'));
}
_location = Random().nextInt(_areas.length);
}
一種更優雅的構建串列的方式是這樣的:
List<Area> = List<Area>.generate(
16,
(int i) => Area(index: i, name: 'Area $i'),
);
順便說一句,您不需要new
Dart (Flutter) 中的關鍵字。
uj5u.com熱心網友回復:
您好,我認為您宣告 2 _areas 您必須在 iniState 方法中的 _areas 之前洗掉 List 字的問題
uj5u.com熱心網友回復:
請參考:
import 'package:flutter/material.dart';
import 'dart:math';
void main() {
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_State createState() => _State();
}
class Area {
int index;
String name;
Color color;
Area(
{this.index = -1,
this.name = 'Area',
this.color = Colors.lightBlueAccent});
}
class _State extends State<MyApp> {
int _location = 0;
List<Area> _areas = [];
@override
void initState() {
//_areas = new List<Area>();
//List _areas = [];
for (int i = 0; i < 16; i ) {
print("function called");
_areas.add(Area(index: i, name: 'Area ${i}'));
print(_areas.length);
}
var rng = Random();
_location = rng.nextInt(_areas.length);
super.initState();
}
Widget _generate(int index) {
return GridTile(
child: Container(
padding: EdgeInsets.all(5.0),
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
),
onPressed: () => _onPressed(index),
child: Text(
_areas[index].name,
textAlign: TextAlign.center,
),
),
));
}
void _onPressed(int index) {
setState(() {
if (index == _location) {
_areas[index].color = Colors.green;
//You won
} else {
_areas[index].color = Colors.red;
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Grid'),
backgroundColor: Colors.deepPurpleAccent,
),
floatingActionButton: FloatingActionButton(
onPressed: () {
print(_areas.length.toString());
},
),
body: Container(
padding: const EdgeInsets.all(32.0),
child: Center(
child: GridView.count(
crossAxisCount: 4,
children: List<Widget>.generate(16, _generate),
),
),
),
);
}
}
uj5u.com熱心網友回復:
請late
像這樣使用late List<Area> _areas
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/481621.html
上一篇:下載并選擇一個powercfg檔案以使用c#匯入它并且下載后不會失去對應用程式的關注
下一篇:返回列表