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
You are getting that error because the getter
isDialogOpen
returns an Optional. This means the return value can either be atrue
,false
or anull
. But, since if-conditions can only work with Booleans, the SDK tells you there will be an error ifisDialogOpen
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-
2-
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.