We are running flutter null safety migration and completed the same but while running the app we are facing Unhandled Exception: type ‘Null’ is not a subtype of type ‘String’
static Future<AppStrings> getAppText(String local) async {
var appTextBox = await Hive.openBox("appTextBox");
AppStrings appText = new AppStrings(
mainPageTitle: appTextBox.get('mainPageTitle'), ==> error on this line
....
);
return appText;}
I’ve tried adding static Future<AppStrings?> && AppStrings? appText as suggested in other solutions but it throws further error
Error: The argument type 'AppStrings?' can't be assigned to the parameter type 'AppStrings' because 'AppStrings?' is nullable and 'AppStrings' isn't.
2
Answers
I don’t know what
AppStrings
is but it seems thatmainPageTitle
in there is aString
andappTextBox.get('mainPageTitle')
is returningnull
and you can’t do that.There’s two possible options:
mainPageTitle
inAppStrings
nullable, by change theString
there toString?
. This probably requires you to make additional changes in your code to handle it being nullable, so it easier to do the second option.null
. This can be done by writingmainPageTitle: appTextBox.get('mainPageTitle') ?? ''
Try this: