xrr2016 / flutter-tree

Flutter tree widget
https://pub.dev/packages/flutter_tree
MIT License
67 stars 41 forks source link

Feed tree form http call #35

Closed ronlinet closed 1 year ago

ronlinet commented 1 year ago

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 :

  final response = await http.get
    (
      Uri.parse("uri"),
      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();
ronlinet commented 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();
            }
          },
        ),

      ),
    );
  }