Unable to assign data property to Nodes individually.

Issue #47 new
suvit sharma created an issue

Hi Creator, thank you very much for creating this awesome package 🙂

could you please help me with this problem, I’m making nodes dynamically from API data, and I need to assign data property to every node separately.
but the package returns some error

type 'List<Node<dynamic>>' is not a subtype of type 'List<Node<String>>?' of 'children'

String is the type of data that I’m assigning to data property of node.

please take a look at data property of the nodes, also when I’m using data at lowest node level i.e.

data: "isCollection"

then the app is working without any errors, but I need data property for every node separately.

My code for creating the nodes.

List<Node> nodes = state.nodes.isEmpty ? state.pathToPractice.subjects! .map((subject) => Node( key: subject.key, icon: Icons.calculate, label: subject.name, data: "isSubject", children: subject.categories?.length != null ? subject.categories! .map((category) => Node( key: "${subject.key}@${category.id}", icon: Icons.language, label: category.name, data: "isCategory", children: category .collections?.length != null ? category.collections! .map((collection) => Node( label: collection.name, icon: Icons .account_balance_outlined, key: "${subject.key}@${category.id}@${collection.id}", data: "isCollection")) .toList() : [], )) .toList() : [])) .toList() : state.nodes;

the attachment is to show what i actually want

Comments (5)

  1. Kevin Armstrong

    I am working on a similar issue. I will be checking in those changes soon. I may do it today. It may not completely resolve your issue though. I will look at your code in a bit to see if I can help.

  2. suvit sharma reporter

    Thanks @Kevin Armstrong , when ever you get time, please do let me know is there any other way in which I can pass icon URL from nodes to node builder.

    Because in icon property of Node I'm only able to provide IconData and not ImageIcon or any other widget.

    For passing URL from Node to nodeBuilder I was using Data property, but if there is any other way of achieving the same, I'm all in.

    Thanks a lot 😊

  3. Kevin Armstrong

    Two things to try here…

    List<Node<String>> nodes =  = state.nodes.isEmpty ? state.pathToPractice.subjects!.map...
    

    or

    var nodes =  = state.nodes.isEmpty ? state.pathToPractice.subjects!.map...
    

  4. Kevin Armstrong

    Suvit, I just noticed the second part of your comment above. Since the data accepts any object, you can create a simple with two strings and pass an instance of that class to the data property. e.g.

    class SubjectData {
      final String url;
      final String type;
    
      const SubjectData({this.url, this.type});
    }
    var subjectData = SubjectData(url: '', type: 'isSubject');
    var categoryData = SubjectData(url: '', type: 'isCategory');
    var collectionData = SubjectData(url: '', type: 'isCollection');
    
    // then do something like
    Node( 
      label: collection.name, 
      icon: Icons .account_balance_outlined, 
      key: "${subject.key}@${category.id}@${collection.id}", 
      data: collectionData
    )
    

    …then you can retrieve the url from the data object

  5. Log in to comment