skip to Main Content

I am creating a world time app and when I am popping the screen from the stack and sending data to the home screen it is transfering a bool object but it is still giving the error: type 'Null' is not a subtype of type 'bool' For the code please access my GitHub repo.

Here’s the relevant part:

import 'package:flutter/material.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/standalone.dart' as tz1;
import 'package:flutter_analog_clock/flutter_analog_clock.dart';

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  Map data = {};
  @override
  Widget build(BuildContext context) {
    data = data.isEmpty
        ? data = ModalRoute.of(context)?.settings.arguments as Map
        : data = data;
    tz.initializeTimeZones();
    var detroit = tz1.getLocation(data["url"]);
    var now = tz1.TZDateTime.now(detroit);
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
            gradient: data["isday"]
                ? const LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                        Color.fromRGBO(57, 59, 245, 1),
                        Color.fromRGBO(62, 103, 238, 1),
                        Color.fromRGBO(84, 171, 230, 1)
                      ])
                : const LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                        Colors.black,
                        Color.fromRGBO(22, 63, 198, 1),
                      ])),
        child: SafeArea(
          child: Column(
            children: [
              const SizedBox(height: 20),
              const Text("World Clock",
                  style: TextStyle(color: Colors.white, fontSize: 20)),
              const SizedBox(height: 60),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(data["location"],
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 70,
                        fontWeight: FontWeight.w500,
                        shadows: <Shadow>[
                          Shadow(
                              offset: Offset(3.0, 6.0),
                              color: Color.fromRGBO(0, 0, 0, 0.17647058823529413))
                        ],
                      )),
                  IconButton(onPressed: () {Navigator.pushNamed(context, "/location");}, icon: Icon(Icons.edit))
                ],
              ),
              const SizedBox(height: 20),
              Text(data["date"],
                  style: const TextStyle(
                      color: Colors.white,
                      fontSize: 20,
                      fontWeight: FontWeight.w500)),
              const SizedBox(height: 40),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 72),
                child: AnalogClock(
                    dateTime: now,
                    hourNumberColor: data["isday"] ? Colors.black : Colors.white,
                    hourHandColor: Colors.white,
                    minuteHandColor: Colors.white,
                    secondHandColor: Colors.red,
                    secondHandLengthFactor: 0.8,
                    centerPointColor: null,
                    hourHandWidthFactor: 0.5,
                    dialColor: Colors.black,
                    hourNumbers: const [
                      '',
                      '',
                      '—',
                      '',
                      '',
                      '|',
                      '',
                      '',
                      '—',
                      '',
                      '',
                      '|'
                    ]),
              ),
              const SizedBox(height: 20),
              Text(data["time"],
                  style: const TextStyle(
                      color: Colors.white,
                      fontSize: 50,
                      fontWeight: FontWeight.w500)),
              ],
          ),
        ),
      ),
    );
  }
}

To reproduce the error on your device follow the given steps:

  1. Access the given repo
  2. Run it
  3. Try to click on the edit icon and search for a location that has a timezone (for eg. Cairo)
  4. Select it.

I was expecting the program to reach the home screen and change data such as the time and other things. I have also checked the type of the error causing variable at different points in the program and it says bool only. The error says it is the world_time/pages/home.dart file and on line 29.

2

Answers


  1. The error in your code is in these lines:

    decoration: BoxDecoration(
       gradient: data["isday"]
          ? const LinearGradient(
    

    The problem is that data["isday"] is null so it cannot evaluate the condition of data["isday"]. Make sure that data["isday"] variable is the correct variable you are accessing and also make sure that it is not null.

    Also as you are assigning your data in this line:

    data = data.isEmpty
            ? data = ModalRoute.of(context)?.settings.arguments as Map
            : data = data;
    

    Are you passing the data correctly from your other route?

    Login or Signup to reply.
  2. at your home.dart code (line 17-19) :

    data = data.isEmpty
            ? data = ModalRoute.of(context)?.settings.arguments as Map
            : data = data;
    

    i think it should be like this :

    data = data.isEmpty ? ModalRoute.of(context)?.settings.arguments as Map : data;
    

    for more null safety :

    var mapArguments = ModalRoute.of(context)?.settings.arguments == null ? <dynamic,dynamic>{} : ModalRoute.of(context)?.settings.arguments as Map;
    
    data = data.isEmpty ? mapArguments : data;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search