gpt4 book ai didi

android - flutter 如何检查 Map 是否为空?

转载 作者:行者123 更新时间:2023-12-04 23:54:51 28 4
gpt4 key购买 nike

map 定义如下:

Map<String,dynamic> allAvailableTime;

当用户初始化页面时 map 应该是空的:

我想在从 map 获取数据之前执行空值检查,但是这些不起作用...

检查 1:

allAvailableTime == null ?

错误:

type 'String' is not a subtype of type 'Widget'

检查 2:

allAvailableTime == {} ?

检查应该返回 True,但它返回 False ..

检查 3:

allAvailableTime.isEmpty ?

错误3:

The getter 'isEmpty' was called on null.
Receiver: null
Tried calling: isEmpty

最佳答案

对 null 检查。它应该可以为空

void main() {

//1. Making Map Nullable
Map<String,dynamic>? allAvailableTime;
print(allAvailableTime==null);
//Output: true


//2. If you dont want to make it nullable
Map<String,dynamic> allAvailableTime1 = {};
print(allAvailableTime1.isEmpty);
//Output: true
}

没有 NullSafety

void main() {

//1. Making Map Nullable
Map<String,dynamic> allAvailableTime;
print(allAvailableTime==null);
//Output: true


//2. If you dont want to make it nullable
Map<String,dynamic> allAvailableTime1 = {};
print(allAvailableTime1.isEmpty);
//Output: true
}

关于android - flutter 如何检查 Map 是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69494734/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com