为什么null变量在条件中分配不为空?



我有一个条件语句保持登录用户在闪屏后的扑动,当列表为空导航到login(),当它有值导航引入,但是,尽管list (values_list_r)为空并且"应该是null(并且打印为null),它被认为是"非null"在navigateAfterSeconds和导航引入(),我的错在哪里??

void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
var a;

String initState() {
super.initState();
setState(() {
//my sqlite class 
ValuesDbprovider vdbhelper = new ValuesDbprovider();
vdbhelper.fetchValues();
//list has returned by fetchvalue() and currently is null and empty
a =values_list_r.map((e) => e.user_no ).toList();
//this printed a:  , means a is null
print("a:$a" );
});
}

Widget build(BuildContext context) {
return MaterialApp(
home: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [Color(0xFFFF1844), Color(0xFFFFD200)])
),
child: SplashScreen(
seconds: 3,
//this is conditional statement for navigation 
navigateAfterSeconds: (a != null ? introduce()  : login()),
loadingText: new Text('calculator',
textAlign: TextAlign.center,
style: new TextStyle(
fontSize: 20, fontWeight: FontWeight.w900, fontFamily: 'yasamin',color: Colors.white,
),),
image: new Image.asset('images/logo_logo.png'),
styleTextUnderTheLoader: new TextStyle()
photoSize: 100.0,
onClick: ()=>print("wellcome"),
loaderColor: Colors.white,
),
),
);
}
} 

我认为有一些最好的方法,如果你想使用数组。您可以使用。

来代替var a。
List<dynamic> a = [];

那么在你的initState中,因为它没有返回任何String值,所以写这个

void initState() {
super.initState();
setState(() {
// Your Sqlite Class
final vdbhelper = new ValuesDbprovider();
final dbResult = await vdbhelper.fetchValues();
// secondary option
List<dynamic> myData = dbResult.map((e) => e.user_no ).toList();
a.addAll(myData);
print("a: $a");
});

注意:你的代码不清楚,a将始终返回null,因为它不清楚values_list_r来自哪里。你正在执行一个方法vdbhelper.fetchValues();,但不把它保存到一个变量。当然a将总是返回null,因为它没有源

最新更新