【发布时间】:2021-05-12 21:34:44
【问题描述】:
我正在颤振中创建一个搜索栏,在执行此操作时我发现了上述错误。它显示icon != null。我不明白为什么它会显示!谁能告诉我如何解决这个问题。它显示以下错误:
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building Consumer<PatientDataNotifier>(dirty, dependencies: [_InheritedProviderScope<PatientDataNotifier>]):
'package:flutter/src/material/icon_button.dart': Failed assertion: line 168 pos 15: 'icon != null': is not true.
The relevant error-causing widget was:
Consumer<PatientDataNotifier> file:///F:/ShortedCubeSolutionProjects/flutter_data_table/lib/homescreen.dart:124:16
When the exception was thrown, this was the stack:
#2 new IconButton (package:flutter/src/material/icon_button.dart:168:15)
#3 HomeScreen.build.<anonymous closure> (package:flutter_data_table/homescreen.dart:130:21)
#4 Consumer.buildWithChild (package:provider/src/consumer.dart:177:19)
#5 SingleChildStatelessWidget.build (package:nested/nested.dart:260:41)
#6 StatelessElement.build (package:flutter/src/widgets/framework.dart:4701:28)
下面是我的代码。
class HomeScreen extends StatelessWidget {
String appBarTitle = 'Patient Details';
TextEditingController nameController = TextEditingController();
var icon;
Widget bodyData(PatientDataNotifier patientDataNotifier, BuildContext context) {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Container(
margin: EdgeInsets.only(top: 20.0, left: 10.0, right: 10.0),
decoration: BoxDecoration(
border: Border.all(width: 1.0, color: Colors.black),
),
child: DataTable(
//..........
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(80),
child: Consumer<PatientDataNotifier>(
builder: (context, patientDataNotifier, _){
return AppBar(
centerTitle: true,
title: Text(appBarTitle),
actions: [
new IconButton(
icon: icon,
onPressed: (){
if(this.icon == Icons.search){
this.icon = new Icon(
Icons.close,
color: Colors.white,
);
this.appBarTitle = new TextFormField(
controller: nameController,
style: new TextStyle(
color: Colors.white
),
decoration: new InputDecoration(
prefixIcon: Icon(Icons.search, color: Colors.white),
hintText: 'Search..',
hintStyle: TextStyle(color: Colors.white),
),
onChanged: (text){
patientDataNotifier.getPatients(text);
},
) as String;
}
}
)
],
);
},
),
),
body: Container(
child: Consumer<PatientDataNotifier>(
builder: (context, patientDataNotifier, _){
return bodyData(patientDataNotifier, context);
},
),
),
);
}
}
请帮我做这件事!我被卡住了!
【问题讨论】:
-
图标变量的变量赋值在哪里?