Flutter同代码跨设备运行 isLoading为false时触发空检查报错
问题描述
本人并非开发新手,长期负责各类错误排查、Bug修复与问题解决工作。近几周遇到一个异常Flutter运行问题:同一份业务代码在其他笔记本、他人的项目环境中均可正常运行,本地此前也曾多次成功跑通该流程,但当前在Android Studio中运行时持续抛出错误,且这些错误在已提交的历史代码版本中不存在。
已核查信息
- 已在代码中补充空判断逻辑,对应初始代码片段如下:
child: SingleChildScrollView( child: isLoading ? Column(
- 已确认
isLoading变量实际运行值为false - 当前开发环境为Android Studio
- 运行报错截图如下:

注:下方代码片段中涉及敏感业务数据的部分已做省略,避免代码篇幅过长,相关业务代码如下:
Widget build(BuildContext context) { return DefaultTabController( length: 1, child: MaterialApp( theme: ThemeData(fontFamily: 'Harmattan',), home: Scaffold( key: _scaffoldKey, appBar: AppBar( actions: [ IconButton(onPressed: (){ Navigator.push( context, new MaterialPageRoute( builder: (context) => MaintenanceDataTypesByDriver())); }, icon: Icon(Icons.view_headline_sharp,size: 40,),) ], title: Center(child: Text( 'enter', style: TextStyle(fontSize: 25, color: Colors.white), ),), leading: Builder( builder: (BuildContext context) { return IconButton( icon: const Icon( Icons.arrow_back_rounded, size: 40, color: Colors.white, ), onPressed: () { //Navigator.pushNamed(context, mainScreen.id); Navigator.push( context, new MaterialPageRoute( builder: (context) => maintenance_screen())); }, ); }, ), ), body: TabBarView( children: [ Center( child: SingleChildScrollView( child: isLoading ? Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ new CircularProgressIndicator(), new Text(" اplease hold.. ") ], ) : new Column( //crossAxisAlignment: CrossAxisAlignment.stretch, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ SizedBox(height: 0), Center( child: Text( " choose an option", style: TextStyle(fontSize: 25,), ), ), SizedBox(height: 5,), // Operations Types Api List New / Container( height: 75, width: 400, decoration: BoxDecoration( color: Colors.blue.withOpacity(0.35), // border color borderRadius: BorderRadius.all(Radius.circular(20)) ), child: Center( child: DropdownButtonHideUnderline( child: DropdownButton( style: TextStyle( fontSize: 20.0, fontWeight: FontWeight.bold ),), ......... }).toList(), onChanged: (String? newVal) { setState(() { print(faultReportingName1.toString()); }); }, value: faultReportingName, //pasing the default id that has to be viewed... //i havnt used something ... //you can place some (id) ), ), ), ), SizedBox(height: 5), Center( child: Text( "press here", style: TextStyle(fontSize: 25,), ), ), SizedBox(height: 5,), // Fault Locations Api List New Container( height: 75, width: 400, decoration: BoxDecoration( color: Colors.blue.withOpacity(0.35), // border color borderRadius: BorderRadius.all(Radius.circular(20)) ), child: Center( child: DropdownButtonHideUnderline( child: DropdownButton( hint: Text("اpress here", style: TextStyle( fontSize: 20.0, fontWeight: FontWeight.bold ),), ..... style: const TextStyle( fontSize: 20.0, fontWeight: FontWeight.bold ), textAlign : TextAlign.end, ), ........... }, value: faultLocationsName, //pasing the default id that has to be viewed... //i havnt used something ... //you can place some (id) ), ), ), ), SizedBox(height: 10), Center( child: Text( " date", style: TextStyle(fontSize: 25), ), ), // Container( width: 300, height: 75, child: TextField( style: TextStyle(color: Colors.black, fontSize: 20), onTap: () { FocusScope.of(context).requestFocus(new FocusNode()); showDatePicker(context: context, initialDate: DateTime.now(), textAlign: TextAlign.center, decoration: InputDecoration( errorStyle: TextStyle(height: 0.1), errorText: _validateStartingDateController ? 'date' : null, alignLabelWithHint: true, //labelText: 'Qty', hintText: '$faultDate1' , hintStyle: TextStyle(), border: OutlineInputBorder( borderSide: BorderSide(color: Colors.yellow, /* 0xfffdbd2a*/width: 20), borderRadius: BorderRadius.all(Radius.circular(15.0)), //borderRadius: BorderRadius.circular(15), ), ), obscureText: false, //controller: myController, ), ), // Center( child: Text( "maintenance ", style: TextStyle(fontSize: 25), ), ), SizedBox(height: 0), TextField( style: TextStyle(fontSize: 20), keyboardType: TextInputType.number, //inputFormatters: [FilteringTextInputFormatter.digitsOnly], controller: opCostController, textAlign: TextAlign.end, decoration: InputDecoration( errorText: _valueValidate ? 'required' : null, alignLabelWithHint: true, //labelText: 'Qty', hintText: 'IQD ', hintStyle: TextStyle(), border: OutlineInputBorder( borderSide: BorderSide(color: Color(0xfffdbd2a), width: 20), borderRadius: BorderRadius.all(Radius.circular(15.0)), //borderRadius: BorderRadius.circular(15), ), ), obscureText: false, //controller: myController, ), SizedBox(height: 0), Center( child: Text( "notes", style: TextStyle(fontSize: 25), ), ), SizedBox(height: 0), TextField( style: TextStyle(fontSize: 20), controller: notesController, textAlign: TextAlign.end, decoration: InputDecoration( errorStyle: TextStyle(height: 1), alignLabelWithHint: true, hintStyle: TextStyle(), border: OutlineInputBorder( borderSide: BorderSide(color: Color(0xfffdbd2a), width: 20), borderRadius: BorderRadius.all(Radius.circular(15.0)), //borderRadius: BorderRadius.circular(15), ), ), obscureText: false, //controller: myController, ), SizedBox(height: 5), Center( child: _file == null ? Text('') : Container( height: 310, margin: EdgeInsets.all(10), decoration: BoxDecoration( image: DecorationImage( fit: BoxFit.fill, image: FileImage(_file!), ), borderRadius: BorderRadius.circular(10), ), //child: Image.asset('images/Logistic.jpg'), ), ), Container( height: 50, width: 200, child: ElevatedButton( //style: ElevatedButton.styleFrom(side: BorderSide(width: 1,style: BorderStyle.solid), style: ButtonStyle( shape: MaterialStateProperty.all< RoundedRectangleBorder>( RoundedRectangleBorder( borderRadius: BorderRadius.circular(50.0), side: BorderSide(color: Colors.white)))), //style: ButtonStyle(shape: OutlinedBorder()), child: Text( 'save', style: TextStyle(color: Colors.white, fontSize: 20), ), onPressed: () { //if(isLoading)return; setState(() { double opCost = double.parse(opCostController.text.trim()); String date = faultDateController.text; int faultLocID = int.parse(faultLocationsName1!.trim()); int faultRepID = int.parse(faultReportingName1!.trim()); String notes = notesController.text.trim(); if(opCost.isNaN || faultReportingName1=='' || faultLocationsName1=='' ||date =='') { print('empty'); return; } else{ upload();
内容的提问来源于stack exchange,提问作者Faith.youssef.abk
相关产品推荐
相关产品推荐

