flutter_tree_view icon indicating copy to clipboard operation
flutter_tree_view copied to clipboard

Retrieve 4 levels of lists from mysql database

Open AL-YAFEA-KHALED opened this issue 3 years ago • 16 comments

Everything was great but if I have 4 different levels of lists from mysql database how to implement it

AL-YAFEA-KHALED avatar May 15 '22 16:05 AL-YAFEA-KHALED

HI @AL-YAFEA-KHALED! Thank you for using the package!

I would need a little bit more information to help you with your issue. Could you post a snippet of code, or include more details of what you are trying to do?

baumths avatar May 15 '22 19:05 baumths

Hi sir Thank you for replying me. Actually I haven't add any code yet I just used your package as it is. But what I'm confused here is that how can bring data from mysql database and fetch them in the tree view. I have 4 tables that contains 4 level of accounts tree so I wanna select them and combine them to list then display them as you did in your package. I hope you can help me in this

AL-YAFEA-KHALED avatar May 17 '22 18:05 AL-YAFEA-KHALED

Hi sir never mind about how I get the four levels can you just tell me how to use Class model as list instead of using map string list as you did in your package. Thanks and sorry for disturbing you

AL-YAFEA-KHALED avatar May 18 '22 15:05 AL-YAFEA-KHALED

Hey @AL-YAFEA-KHALED!

I don't think I quite understand what you're trying to achieve...

To create a simple tree:

Future<void> createSubtree(TreeNode? parent) async {
  final models = await getDataFromDatabase(
    id: parent?.id ?? yourTopLevelId,
  );

  if (models.isEmpty) {
    // Exit recursion if no more models are found
    return;
  }

 for (final model in models) {
    final node = TreeNode (
      id: model.id,
      label: model.label,
      data: model,
    ),

    parent?.addChild(node);
    await createSubtree(node);
  }
}

final root = TreeNode(id: "your top level id here");
await createSubtree(root);

// Then pass `root` into the tree controller

I don't know if it's what you're looking for but without more information and some code snippets, I may not be able to help you. Let me know if this helps.

baumths avatar May 18 '22 21:05 baumths

Hi sir, I followed your code with a little bit changes. It worked and display in the tree view but it only display one item like 1- name while I have four items in the list This is the code Screenshot 2022-05-21 at 3 05 31 AM Can you help me in this. Thanks and sorry for disturbing you

AL-YAFEA-KHALED avatar May 21 '22 00:05 AL-YAFEA-KHALED

Hey @AL-YAFEA-KHALED!

The only problem that I can spot in your code is the hard-coded final level = level1, every time this function is executed, it loops through the same list, you must update that list before the next execution.

Have a try with the following update:

Future<void> getTree<T>(TreeNode? parent, List<T> levels) async {
  if (levels.isEmpty) return;

  for (final T level in levels) {
    final node = TreeNode (
      id: level.levelNumber1.toString(),
      label: level.levelName1.toString(),
      data: level,
    );

    parent?.addChild(node);
    await getTree<T>(node, <insert next level list here>);
  }
}

final root = TreeNode(...);
await getTree<your model type here>(root, level1);

Try this out and let me know if it works.

Edit: you can remove the generic <T> if you put your model type directly on the List<T>.

baumths avatar May 21 '22 00:05 baumths

Sir actually the code doesn't work. But I figured out that the await is stopping the loop from continue looping and it just stopped at once loop. When I disabled the all await line at the end it works fine and displayed all my items. But the next step is I wanna get now sublevels. I will give try I hope I can handle it otherwise I will comment to you sir

AL-YAFEA-KHALED avatar May 21 '22 01:05 AL-YAFEA-KHALED

Yeah... The async part of it is not necessary, hope you get things working!

baumths avatar May 21 '22 02:05 baumths

You did great job in this package. It's really amazing and very helpful. I hope you go ahead and create more packages. I still didn't fetch sublevels yet I will do it tonight and update you sir

AL-YAFEA-KHALED avatar May 21 '22 14:05 AL-YAFEA-KHALED

Sir, I'm facing problem here. As we discussed before I was able to display the first level in the tree view with the previous code. For the second level I tried to add it using parent?.add children(.....). But it display the items in level 2 as top level not displaying them as nested items from level1. Then I figured out that this code parent?.children.foreach(getTree) Is the code that make the items nested but this make the app hanging up. I think it's hanging up because it's not finding nested items in level one so how can I make my 4 levels nested in one map string list. Then I can use one list for all my lists

AL-YAFEA-KHALED avatar May 24 '22 18:05 AL-YAFEA-KHALED

Hey @AL-YAFEA-KHALED!

Could you please show me how is your code at the moment? Because this doesn't seem to be a problem with the package.

If you could provide a code snippet, I'd gladly help you find a solution, but with the lack of information I might not be able to.

baumths avatar May 24 '22 21:05 baumths

`Future getTree(TreeNode? parent) async { final levels = level1; if (levels.isEmpty) return; for (final level in levels) { final node = TreeNode ( id: level.levelNumber1.toString(), label: level.levelName1.toString(), data: level, ); parent?.addChild(node); } }

final root = TreeNode(...); await getTree(root);`

This works fine for top-level now I have 3 more levels under top-level. I want to display them as nested levels based on level names

AL-YAFEA-KHALED avatar May 25 '22 01:05 AL-YAFEA-KHALED

Hey!

Take a look at my previous answer, your level variable is hard-coded inside the getTree function, you have to reassign that variable to the next levels when done creating nodes. The code I shared previously takes this into account:

Future<void> getTree<T>(TreeNode? parent, List<T> levels) async {
  if (levels.isEmpty) return;

  for (final T level in levels) {
    final node = TreeNode (
      id: level.levelNumber1.toString(),
      label: level.levelName1.toString(),
      data: level,
    );

    parent?.addChild(node);
    await getTree<T>(node, <insert level 2 list here>);
  }
}

final root = TreeNode(...);
await getTree(root, level1);

baumths avatar May 25 '22 02:05 baumths

Sir, I did as your exact code but it show errors in

level.levelNumber.toString(); level.levelName.toString()` They are not defined. The code isn't true. If you can assume that you have the four levels and you want to add them to tree view how can you do it in your package sir

AL-YAFEA-KHALED avatar May 25 '22 14:05 AL-YAFEA-KHALED

Sir I solved the issue in different way. Now I wanna ask you. We have got function to add child when we click on node id it shows dialog to add new child. Can we add child to specific node id without out selecting node id?

AL-YAFEA-KHALED avatar Jun 05 '22 23:06 AL-YAFEA-KHALED

Hey @AL-YAFEA-KHALED!

It should be straightforward, take a look at the find method of the TreeViewController class.

The find method traverses the tree looking for a node which id matches the id passed to it and then returns the found node, or null if no node is found with the given id.

baumths avatar Jun 06 '22 00:06 baumths