1

this is my complete code...

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class DialogHelper{
  //show error dialog
 static void showErrorDialog({String title='error',String description='Something went wrong'})
  {
    Get.dialog(
      Dialog(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(title,style: Get.textTheme.headline4,),
              Text(description,style: Get.textTheme.headline6,),
              ElevatedButton(onPressed: () {
                if (Get.isDialogOpen) Get.back();
              },
                  child: Text('okay')),
            ],
          ),
        ),
      ),
    );

  }
}

And I got this error

19:25: Error: A value of type 'bool?' can't be assigned to a variable of type 'bool' because 'bool?' is nullable and 'bool' isn't. if (Get.isDialogOpen) Get.back();

I got error on the line if condition Get.isDialogOpen

Filburt
  • 17,626
  • 12
  • 64
  • 115

1 Answers1

5

You are getting that error because the getter isDialogOpen returns an Optional. This means the return value can either be a true, false or a null. But, since if-conditions can only work with Booleans, the SDK tells you there will be an error if isDialogOpen returns a null.

So to fix that, either you tell the compiler that you are sure your getter will never return a null, or you have to give a default value in case a null is returned from .isDialogOpen. We do it like this respectively;

1-

  Get.isDialogOpen! \\ this means you are sure a null can't be returned

2-

 Get.isDialogOpen ?? false \\ this means incase a null is returned use false   

Note: If you use number 1, and a null is eventually returned, your code will crash at run-time. To avoid that, you can tell the compiler to call the isDialogOpen only if it has been initialized. i.e.

Get?.isDialogOpen ?? false \\If isDialogOpen is not initialized, false will be used
Loïc Fonkam
  • 2,284
  • 11
  • 25