在颤振中获取 api 时,它显示小部件库捕获的异常 错误状态:无元素



实际问题是什么?

class Homenotifier extends ChangeNotifier {  
Homerepositery _service = Homerepositery();
List _getAllLocationdata = [];
bool _loading = false;
List get getAllocation => _getAllLocationdata;
bool get loading => _loading;

void setLoading(bool status) {
_loading = status;
notifyListeners();   
}

Future getLocations() async {
try {
setLoading(true);
final response = await _service.getLocations();
_getAllLocationdata = response["data"] as List;
setLoading(false);
log(_getAllLocationdata.runtimeType.toString());
log(_getAllLocationdata.length.toString());
} catch (e) {
setLoading(false);
log(e.toString());
}
}
}

在视图部分

Class AllLocation extends StatefulWidget {
const AllLocation({Key? key}) : super(key: key);
@override
State<AllLocation> createState() => _AllLocationState();
}
class _AllLocationState extends State<AllLocation> {
@override
void initState() {
Future.delayed(Duration.zero, () {
final provider = Provider.of<Homenotifier>(context, listen: false);
provider.locationdatas;
});
super.initState();

}


@override
Widget build(BuildContext context) {
final provider = Provider.of<Homenotifier>(context, listen: false);
return Scaffold(
body: Expanded(
child: Consumer<Homenotifier>(
builder: (context, value, child) => value.loading
? const Center(
child: CircularProgressIndicator(),
)   
: Card(
child: ListView.separated(
separatorBuilder: (context, index) {
return const Divider();
},
itemCount: provider.getAllocation.length,
itemBuilder: (context, index) => GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LiveCountView(
datas: provider.getAllocation[index],
),
));
},
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width:
MediaQuery.of(context).size.width * 0.2,
height: 60,
child: Row(
//mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Icon(Icons.location_on),
Flexible(
child: Text(
provider.getAllocation[index]
['zone']
.toString(), 
overflow: TextOverflow
.ellipsis, //this one now works :)
style: const TextStyle(
fontSize: 14,
color: Colors.black,
fontWeight: FontWeight.w400)),
),
],
),
),
Container(
width:
MediaQuery.of(context).size.width * 0.5,
height: 60,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text("MPC as on"),
Text(
"${provider.getAllocation[index]['date'].toString()} at ${provider.getAllocation[index]["time"]}")
],
),
),
Container(
width:
MediaQuery.of(context).size.width * 0.2,
height: 50,
child: Center(
child: Text(provider.getAllocation[index]
["count"]
.toString())),
),
],
),
),
),
),
),
),

显示类似

═════════ 控件库捕获的异常 ═════════════════════════════════

══ 错误状态:无元素 导致错误的相关小部件是 AllLocation"。

从互联网获取数据时不要使用 ListView.separd()。 始终使用 futurebuider 或 stream builder,实现这些构建器将解决您的问题

相关内容

  • 没有找到相关文章

最新更新