我要选中一个复选框。这是我运行代码时遇到的错误:
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 285 pos 10: 'data != null'
这是我的代码:
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'datagejala.dart';
class Diagnosis2 extends StatefulWidget {
@override
_Diagnosis2State createState() => _Diagnosis2State();
}
class _Diagnosis2State extends State<Diagnosis2> {
//initState
bool selected = false;
var userStatus = List<bool>();
Future<List<DataGejala>> getContactsFromJSON(BuildContext context) async{
String jsonString = await DefaultAssetBundle.of(context).loadString("assets/data/penyakit.json");
List<dynamic> raw = jsonDecode(jsonString);
return raw.map((f) => DataGejala.fromJSON(f)).toList();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Select City'),
),
body: Container(
child: FutureBuilder(
future: getContactsFromJSON(context),
builder: (BuildContext context, AsyncSnapshot snapshot) {
print(snapshot.data);
if (snapshot.data == null) {
return Container(child: Center(child: Text("Loading...")));
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(snapshot.data[index].g_nama),
trailing: Checkbox(
value: userStatus[index],
onChanged: (bool val) {
setState(() {
userStatus[index] = !userStatus[index];
});
}),
);
},
);
}
},
),
),
);
}
}
答案 0 :(得分:0)
该错误消息表示您尝试用于“文本”窗口小部件的值为null,因此您的错误与复选框无关,而是您需要重新检查“ snapshot.data [index] .g_nama”的原因返回null,而不是您想要的字符串。