颤振类成员列表即使在初始化后也返回null



下面是一个文件夹模型类:

import 'package:flutter/material.dart';
import 'block_model.dart';
class Folder {
Folder({
this.title = '',
}) {
this.folderContent = this.folderContent ?? <Block>[];
}
String title;
List<Block> folderContent = [];
String id;
void renameFolder(String value) {
title = value;
}
Folder.fromJson(Map<String, dynamic> json) {
this.id = '${json['id']}';
this.title = json['title'];
this.folderContent = json['folderContent'];
}
Map<String, dynamic> toJson() {
Map<String, dynamic> folderJson = {};
folderJson['id'] = this.id;
folderJson['title'] = this.title;
folderJson['folderContent'] = this.folderContent;
return folderJson;
}
}

文件夹是通过这样的API调用创建的:

Future<void> createLibraryFolder(String title) async {
const String createFolderURL = '/api/TimelineLibrary/createFolder';
Folder newFolder = Folder(title: title);
var res =
await request.postRequest(newFolder.toJson(), createFolderURL, false);
print(res);
}

有一个列表叫做&;folders&;它映射从库视图API获取的文件夹。问题是在创建文件夹时folderContent列表是空的(这很好,应该是),但是当这些folder对象被映射到创建视图时,folderContent返回null。下面是一个文件夹视图的代码:

List<Widget> createFolders() {
return _timeLineController.folders.map((Folder folder) {
// if (folder.folderContent == null) {
//   folder.folderContent = <Block>[];
// }
Widget folderWidget = Container(
width: 300,
margin: const EdgeInsets.all(3),
child: FolderWidget(folder: folder),
);
return GestureDetector(
onLongPressStart: (LongPressStartDetails details) {
showMenu(
color: kLightAccent,
context: context,
position: RelativeRect.fromLTRB(
details.globalPosition.dx,
details.globalPosition.dy,
details.globalPosition.dx,
details.globalPosition.dy),
items: <PopupMenuEntry<String>>[
PopupMenuItem(
value: 'Menu',
child: MenuItem(
'Edit',
() {
Navigator.pop(context);
},
),
),
],
);
},
onPanUpdate: (DragUpdateDetails dragDetails) {
if (dragDetails.delta.dx > 0) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Delete Folder?'),
content: Container(
height: 50,
width: 50,
alignment: Alignment.center,
child: Row(
children: <Widget>[
Spacer(),
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(kOrange),
foregroundColor:
MaterialStateProperty.all<Color>(
Colors.white),
),
onPressed: () async {
Navigator.pop(context);
await deleteLibraryFolder(folder.id);
},
child: Text('Yes')),
SizedBox(width: 10),
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(kOrange),
foregroundColor:
MaterialStateProperty.all<Color>(
Colors.white),
),
onPressed: () {
Navigator.pop(context);
},
child: Text('No')),
],
),
),
);
});
}
},
onTap: () {
folderID = folder.id;
setState(() {
isCheck = true;
});
},
child: folderWidget,
);
}).toList();
}

当这些文件夹像上面所示的那样被映射时,folderContent是空的,根据类模型中完成的初始化不应该是空的。我已经尝试将folderContent初始化为空列表(参见FolderView中的注释代码),但这并不适合我的要求,因为每次小部件重建时列表都会变为空。当我尝试使用map函数时,问题出现了,该函数以某种方式将folderContent设置为null。我该如何解决这个问题?

编辑:调试控制台输出当我尝试创建文件夹:

I/flutter (22459): []
I/flutter (22459): {id: 57}

这表明空列表已初始化,这正是我想要的。

I/flutter (22459): 57
I/flutter (22459): test
I/flutter (22459): null

这是当文件夹被映射到'folderContent'为空的列表时。"test"是文件夹的标题。

你能试试吗?

Folder({
this.title = '',
this.folderContent = const [],
});
String title;
List<Block> folderContent;
String id;

通常要检查变量的赋值,在调用构造函数时不需要使用函数,可以直接通过构造函数进行检查。如果您稍后需要使用add或其他内容修改列表,请检查列表是否为空,然后如果是空的,则使用=代替add

最新更新