我是初学者,我的http请求有问题,当我想在我的DropdownButton中添加此http请求(我将其放在名为deviceGet的列表中)时,甚至打印它以查看它:我有此错误:
RangeError(index) : Invalid value : Valid value range is empty : 0
这是所有错误消息:
Exception caught by widgets library ═══════════════════════════════════
The following RangeError was thrown building Builder:
RangeError (index): Invalid value: Valid value range is empty: 0
The relevant error-causing widget was
MaterialApp
package:chat/main.dart:180
When the exception was thrown, this was the stack
#0 List.[] (dart:core-patch/growable_array.dart:149:60)
#1 _MyListScreenState.initState
package:chat/main.dart:226
#2 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4428
#3 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4274
#4 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3269
...
════════════════════════════════════════════════════════════════════════════════
Reloaded 1 of 591 libraries in 363ms.
flutter: 0
════════ Exception caught by widgets library ═══════════════════════════════════
The following RangeError was thrown building Builder:
RangeError (index): Invalid value: Valid value range is empty: 0
The relevant error-causing widget was
MaterialApp
package:chat/main.dart:180
When the exception was thrown, this was the stack
#0 List.[] (dart:core-patch/growable_array.dart:149:60)
#1 _MyListScreenState.initState
package:chat/main.dart:225
#2 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4428
#3 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4274
#4 Element.inflateWidget
我不明白此错误,因为如果我把它放在体内,我就能看到我的要求
ListView.builder(
itemCount: deviceGet.length,
itemBuilder: (context, index) {
return ListTile(
title: Text("Num $index " + device[index].commands[0].id));
title: Text("Num $index " + deviceGet[index].name));
或者如果我放
Text(deviceGet[0].name)
但有警告
这是我的代码:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'Get.dart';
import 'Device.dart';
import 'Commands.dart';
import 'deviceData.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
build(context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'My App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyListScreen(),
);
}
}
class MyListScreen extends StatefulWidget {
@override
createState() => _MyListScreenState();
}
class _MyListScreenState extends State {
//
List<deviceData> dataDevice = deviceData.getDevicesData();
List<DropdownMenuItem<deviceData>> listDropDevice;
deviceData deviceSelection;
//
var deviceGet = new List<Device>();
GetDevice() {
GET.getDevice().then((response) {
setState(() {
Iterable list = json.decode(response.body);
deviceGet = list.map((model) => Device.fromJson(model)).toList();
});
});
}
void initState() {
super.initState();
GetDevice();
//print(deviceGet[0].name);
// add in the dropDown
for (int i = 0; i < 5;) {
print(i);
dataDevice.add(deviceData("Device" + i.toString()));
dataDevice.add(deviceData(deviceGet[0].name));
i = i + 1;
}
listDropDevice = buildDropdownMenuItems(dataDevice);
deviceSelection = listDropDevice[0].value;
}
List<DropdownMenuItem<deviceData>> buildDropdownMenuItems(List devices) {
List<DropdownMenuItem<deviceData>> items = List();
for (deviceData device1 in devices) {
items.add(
DropdownMenuItem(
value: device1,
child: Text(device1.name),
),
);
}
return items;
}
onChange(deviceData selectionUtilisateur) {
setState(() {
deviceSelection = selectionUtilisateur;
});
}
@override
build(context) {
return Scaffold(
appBar: AppBar(
title: Text("Device List"),
),
body: Column(children: <Widget>[
//Text(deviceGet[0].name),
DropdownButton(
value: deviceSelection,
items: listDropDevice,
onChanged: onChange,
),
]));
}
}
谢谢您的帮助
GetDevice()是异步的方法,因此从json获取数据需要花费时间,并且在请求中[[deviceGet为空,因此会产生范围错误。
您可以使用await和async *使GetDevice()方法异步,并在调用该方法时使用.then方法,然后访问值。此外,请确保您正在GetDevice方法中获取数据。