Flutter 颤振';二传手';任务=';被调用为空

Flutter 颤振';二传手';任务=';被调用为空,flutter,dart,null,Flutter,Dart,Null,各位!! 我正在学习颤振/飞镖,在实例化和初始化方面一直存在问题。 目前我正在编写一个可重新排序的列表,但由于某些原因,我无法填充该列表,因为我的实例有一些问题。。。(我认为问题在于以下几行) 相关代码: import 'package:flutter/material.dart'; import './bottomNavigationBar.dart'; import './ViewFeed.dart'; import './ViewNewTodo.dart'; void main() =&

各位!! 我正在学习颤振/飞镖,在实例化和初始化方面一直存在问题。 目前我正在编写一个可重新排序的列表,但由于某些原因,我无法填充该列表,因为我的实例有一些问题。。。(我认为问题在于以下几行)

相关代码:

import 'package:flutter/material.dart';
import './bottomNavigationBar.dart';
import './ViewFeed.dart';
import './ViewNewTodo.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      //home: MyScaffold(),
      initialRoute: '/',
      routes: {
        '/':(context) => MyFeed(),
        '/toDo':(context) => MyScaffold(),
        '/newToDo':(context) => MyNewTodo(),
      },
    );
  }
}

class MyScaffold extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My ToDoS'),
      ),
      body: MyReorderableTaskList(),
      floatingActionButton: MyFloatingActionButtonNewTodo() ,
      bottomNavigationBar: MyBottomNavigationBar(),
    );
  }
}

//Reorderable list elements
//FloatingActionButton
class MyFloatingActionButtonNewTodo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      child: Icon(Icons.add),
      tooltip: 'Idea',
      onPressed: ()=>{ Navigator.pushNamed(context, '/newToDo') },
    );
  }
}

class MyTask{
  String task;
  MyTask(this.task);
}
//ReorderableListView  implementation

class MyReorderableTaskList extends StatefulWidget {
  @override
  _MyReorderableTaskListState createState() => _MyReorderableTaskListState();
}

class _MyReorderableTaskListState extends State<MyReorderableTaskList> {
  List<String> tasks = [];
  MyTask t;

  void initState(){
    tasks = [ t.task = 'Buy ice cream', t.task = 'Learn Flutter', t.task = 'Read books' ];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    return ReorderableListView(
        onReorder: _onReorder,
        children: List.generate(
            tasks.length,
              (index){ return MyListView(index, Key('$index'), tasks ); }
              ),
          );
  }
  void _onReorder(int oldIndex, int newIndex){
    setState(() {
        if(newIndex > oldIndex) { newIndex -= 1; }
        final String item = tasks.removeAt(oldIndex);
        tasks.insert(newIndex, item);
    });
  }
}

class MyListView extends StatefulWidget {
  final int index;
  final Key key;
  final List<String> listTasks;
  MyListView(this.index, this.key, this.listTasks);
  @override
  _MyListViewState createState() => _MyListViewState();
}

class _MyListViewState extends State<MyListView> {
  @override
  Widget build(BuildContext context) {
    return Card(
         margin: EdgeInsets.all(6),
          child: InkWell(
            splashColor: Colors.blue,
            onTap: ()=>{ MaterialState.focused },
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                ListTile(
                  title: Text('Tarea: ${widget.listTasks[widget.index]} | ${widget.index}',),
                ),
              ],
            ),
          ),
        );
      }
}



导入“包装:颤振/材料.省道”;
导入“/bottomNavigationBar.dart”;
导入“./ViewFeed.dart”;
导入“/ViewNewTodo.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
//主页:MyScaffold(),
initialRoute:“/”,
路线:{
“/”:(上下文)=>MyFeed(),
“/toDo”:(上下文)=>MyScaffold(),
“/newToDo”:(上下文)=>MyNewTodo(),
},
);
}
}
类MyScaffold扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“我的待办事项”),
),
正文:MyReorderableTaskList(),
floatingActionButton:MyFloatingActionButtonNewTodo(),
bottomNavigationBar:MyBottomNavigationBar(),
);
}
}
//可重排序列表元素
//浮动操作按钮
类MyFloatingActionButtonNewTodo扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回浮动操作按钮(
子:图标(Icons.add),
工具提示:“创意”,
onPressed:()=>{Navigator.pushNamed(上下文,'/newToDo')},
);
}
}
类MyTask{
字符串任务;
MyTask(this.task);
}
//ReorderableListView实现
类MyReorderableTaskList扩展了StatefulWidget{
@凌驾
_MyReorderableTaskListState createState()=>\u MyReorderableTaskListState();
}
类_MyReorderableTaskListState扩展状态{
列出任务=[];
MYT任务;
void initState(){
tasks=[t.task='Buy ice cream',t.task='Learn flatter',t.task='Read books'];
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回ReorderableListView(
onReorder:_onReorder,
子项:List.generate(
任务。长度,
(索引){返回MyListView(索引,键('$index'),任务);}
),
);
}
void _onReorder(int oldIndex,int newIndex){
设置状态(){
如果(newIndex>oldinex){newIndex-=1;}
最后一个字符串项=tasks.removeAt(oldIndex);
任务。插入(新索引,项);
});
}
}
类MyListView扩展了StatefulWidget{
最终整数指数;
最后一键;
最后任务清单;
MyListView(this.index、this.key、this.listTasks);
@凌驾
_MyListViewState createState()=>\u MyListViewState();
}
类_MyListViewState扩展状态{
@凌驾
小部件构建(构建上下文){
回程卡(
保证金:所有(6),
孩子:InkWell(
颜色:颜色。蓝色,
onTap:()=>{MaterialState.focused},
子:列(
mainAxisSize:mainAxisSize.min,
儿童:[
列表砖(
标题:Text('Tarea:${widget.listTasks[widget.index]}|${widget.index}',),
),
],
),
),
);
}
}

mytaskt为空
t实际上还不是什么。您必须创建一个新对象
MyTask()
并将其分配给
t

换成

MyTask t = new MyTask("");
编辑:更透彻的解释

您不能只声明
MyTask t
然后尝试使用
t
,因为您尚未说出
t
是什么。这就像声明
inta
然后尝试执行
打印(a+5)
。您不能这样做,因为您尚未为
a
赋值。类似地,
MyTask t
表示您已经创建了类型为
MyTask
的名为
t
的变量。换句话说,您已经声明了一个变量
t
。但是您仍然必须初始化它,换句话说,为
t
分配类型为
MyTask
的值,否则
t
的值将为
null

总而言之

MyTask t;
t.task = "Hello";
不会起作用,因为这就像

int a;
int b = a + 5;
无法将5添加到,因为尽管您已声明变量
a
,但尚未初始化它,因此它还没有值

同样,您也无法访问
t
.task
属性,因为您尚未说明
t
是什么
t
只是
null

因此,您必须通过实例化一个新的
MyTask
对象来初始化
t
-

MyTask t=newmytask(“”)

括号内需要
,因为MyTask类构造函数需要一个参数

class MyTask{
  String task;
  MyTask(this.task); // Parameter required to assign to the String task
}

mytaskt为空
t实际上还不是什么。您必须创建一个新对象
MyTask()
并将其分配给
t

换成

MyTask t = new MyTask("");
编辑:更透彻的解释

您不能只声明
MyTask t
然后尝试使用
t
,因为您尚未说出
t
是什么。这就像声明
inta
然后尝试执行
打印(a+5)
。您不能这样做,因为您尚未为
a
赋值。类似地,
MyTask t
表示您已经创建了类型为
MyTask
的名为
t
的变量。换句话说,您已经声明了一个变量
t
。但是您仍然必须初始化它,换句话说,为
t
分配类型为
MyTask
的值,否则
t
的值将为
null

总而言之

MyTask t;
t.task = "Hello";
不会起作用,因为这就像

int a;
int b = a + 5;
无法将5添加到,因为尽管您已声明变量
a
,但尚未初始化它,因此它还没有值

同样地,你也不可能有一个