Closed ronlinet closed 1 year ago
I got it work....
The http query must be wrapped in a Future<List<TreeNodeData>>
and the body needs FutureBuilder<List<TreeNodeData>?>
.
Getting data set.
get getVideosFromWebApi async {
final response = await http.get
(
Uri.parse("${ApiPath.mitVideoLinksUri}/${AppVar.apiKey}"),
headers: {"Accept": "application/json", "Content-Type": "application/json"}
);
final Iterable mapCollection = jsonDecode(response.body);
List<TreeNodeData> treeData = List.generate (
mapCollection.length,
(index) => mapServerDataToTreeData(mapCollection.toList()[index]),
).toList();
return treeData;
}
Widget itself
@override
Widget build(BuildContext context) {
return
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: const Text('Flutter Tree Example')),
body:
FutureBuilder<List<TreeNodeData>?>(
future: getVideosFromWebApi,
builder: (context, snapshot) {
if (snapshot.hasData) {
// Build the TreeView using the data obtained from the Future
return Scaffold(
body:
TreeView(
data: snapshot.data!,
lazy: true,
load: _load,
showActions: true,
showCheckBox: true,
showFilter: true,
onLoad: (node) {
print('onLoad');
print(node);
},
onCheck: (checked, node) {
print('checked');
print('onCheck');
print(node);
},
onCollapse: (node) {
print('onCollapse');
print(node);
},
onExpand: (node) {
print('onExpand');
print(node);
},
onRemove: (node, parent) {
print('onRemove');
print(node);
},
onTap: (node) {
print('onTap');
print(node);
},
),);
} else if (snapshot.hasError) {
// Handle the error
return Text('An error occurred: ${snapshot.error}');
} else {
// Show a loading indicator while waiting for the data
return CircularProgressIndicator();
}
},
),
),
);
}
I have tried to feed the list from a http call but I am getting a subtype error :
Futrue<dynamic> is not List<TreeNodeData>
.The code :