0

我在flutter上创建了一个多页app来根据位置来判断时间,但是开启代码的时候,这个页面有问题,我尝试修复问题,但是错误依然存在"type' Null' 不是 'bool' 类型的子类型”我将在评论中包含其他页面。

 import 'package:flutter/material.dart';
    class Home extends StatefulWidget {
      @override
      _HomeState createState() => _HomeState();
    }
    
    class _HomeState extends State<Home> {
       Map? data={};
      @override
    
      Widget build(BuildContext context) {
    
    
           if (data!= null) {
            data = ModalRoute.of(context)!.settings.arguments as Map?;}
    
        // set background image
        final  String? bgImage = data?['isDayTime'] ? 'day.png' : 'night.png';
        final Color? bgColor =  data?['isDayTime'] ? Colors.blue : Colors.indigo[700];
    
         return Scaffold(
           backgroundColor: bgColor,
           body: SafeArea(
            child: Container(
              decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('asset/$bgImage'),
                    fit: BoxFit.cover,
                  )
              ),
              child: Padding(
                padding: const EdgeInsets.fromLTRB(0, 120.0, 0, 0),
                child: Column(
                  children: <Widget>[
                    FlatButton.icon(
                      onPressed: () async {
                        dynamic result = await Navigator.pushNamed(context, '/location');
                        if(result != null){
                          setState(() {
                            data = {
                              'Time': result['Time'],
                              'Location': result['Location'],
                              'isDayTime': result['isDayTime'],
                              'Flag': result['Flag']
                            };
                          });
                        }
                      },
                      icon: Icon(
                        Icons.edit_location,
                        color: Colors.grey[300],
                      ),
                      label: Text(
                        'Edit Location',
                        style: TextStyle(
                          color: Colors.grey[300],
                        ),
                      ),
                    ),
                    SizedBox(height: 20.0),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text(
                          data?['Location'],
                          style: TextStyle(
                            fontSize: 28.0,
                            letterSpacing: 2.0,
                            color: Colors.white,
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 20.0),
                    Text(
                        data?['Time'],
                        style: TextStyle(
                            fontSize: 66.0,
                            color: Colors.white
                        )
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      }
    
    }
4

3 回答 3

0

您的数据是 Map 类型,如果数据没有isDayTime字段而不是返回null,并且三元运算符决定不能在null.
因此,要解决此问题,您可以检查您的数据映射是否具有使用的字段,data.containsKey('isDayTime')或者您可以使用??(null 感知运算符)分配一个fallback值,以便您的三元运算符始终具有boolean值。

于 2022-03-04T11:39:18.973 回答
0

代替

        final  String? bgImage = data?['isDayTime'] ? 'day.png' : 'night.png';
    final Color? bgColor =  data?['isDayTime'] ? Colors.blue : Colors.indigo[700];

        final  String? bgImage = (data?['isDayTime'] ?? false) ? 'day.png' : 'night.png';
    final Color? bgColor =  (data?['isDayTime'] ?? false) ? Colors.blue : Colors.indigo[700];

而且也代替

setState(() {
                        data = {
                          'Time': result['Time'],
                          'Location': result['Location'],
                          'isDayTime': result['isDayTime'],
                          'Flag': result['Flag']
                        };
                      });

setState(() {
                        data = {
                          'Time': result['Time'],
                          'Location': result['Location'],
                          'isDayTime': result['isDayTime'] ?? false,
                          'Flag': result['Flag']
                        };
                      });
于 2022-03-04T10:59:09.383 回答
0

1-data?['isDayTime'](data?['isDayTime']?? false)

2-我认为变量 (isDayTime) 有问题,所以你不应该使用 (late) 键,所以如果你确定 100% 你会声明这个变量,所以如果它没有再次更改,则删除 late 并将其更改为 final

3-转换这张地图?数据={}; 映射数据={}; 因为这已经被声明并查看此链接https://dart.dev/null-safety/understanding-null-safety

于 2022-03-04T10:52:37.573 回答