Android Studio throws this following NullSafety error on split
in Flutter.
The method 'split' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!'). Open documentation
But none of the suggested fixes work. Heres' the code;
FutureBuilder(
future: jwtOrEmpty,
builder: (context, snapshot) {
if(!snapshot.hasData) return CircularProgressIndicator();
if(snapshot.data != "") {
var str = snapshot.data;
var jwt = str.split(".");
if(jwt.length !=3) {
return LoginPage();
} else {
var payload = json.decode(ascii.decode(base64.decode(base64.normalize(jwt[1]))));
if(DateTime.fromMillisecondsSinceEpoch(payload["exp"]*1000).isAfter(DateTime.now())) {
return HomePage(str, payload);
} else {
return LoginPage();
}
}
} else {
return LoginPage();
}
}
),
I'm guessing it's because the returned snapshot.data
can't be null. But I'm not sure what I can do about it. I tried adding "!" and "?" to snapshot.data
, str
, jwt
and split
but nothing works.