Get with Flutter a nested JSON from Firebase DataSnapshot2019 Community Moderator ElectionParsing values from a JSON file?Returning JSON from a PHP ScriptHow to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?How do I get ASP.NET Web API to return JSON instead of XML using Chrome?Getting JSON Key Value from Firebase DatasnapshotHow do I get specific values from a datasnapshot in flutter?How to loop through a Firebase datasnapshot sub childrens? FlutterFlutter: The method forEach isn't defined for the class DataSnapshotFlutter Nested JSON ParsingFlutter get data from firebase without data key issues

In Aliens, how many people were on LV-426 before the Marines arrived​?

Recruiter wants very extensive technical details about all of my previous work

In the 1924 version of The Thief of Bagdad, no character is named, right?

gerund and noun applications

Comment Box for Substitution Method of Integrals

Variable completely messes up echoed string

What does "Four-F." mean?

Does multi-classing into Fighter give you heavy armor proficiency?

What is the term when voters “dishonestly” choose something that they do not want to choose?

Can you move over difficult terrain with only 5 feet of movement?

Are dual Irish/British citizens bound by the 90/180 day rule when travelling in the EU after Brexit?

Help rendering a complicated sum/product formula

The average age of first marriage in Russia

I got the following comment from a reputed math journal. What does it mean?

Unfrosted light bulb

Knife as defense against stray dogs

How to define limit operations in general topological spaces? Are nets able to do this?

What should I install to correct "ld: cannot find -lgbm and -linput" so that I can compile a Rust program?

How do hiring committees for research positions view getting "scooped"?

In what cases must I use 了 and in what cases not?

Hausdorff dimension of the boundary of fibres of Lipschitz maps

Suggestions on how to spend Shaabath (constructively) alone

A Ri-diddley-iley Riddle

Print last inputted byte



Get with Flutter a nested JSON from Firebase DataSnapshot



2019 Community Moderator ElectionParsing values from a JSON file?Returning JSON from a PHP ScriptHow to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?How do I get ASP.NET Web API to return JSON instead of XML using Chrome?Getting JSON Key Value from Firebase DatasnapshotHow do I get specific values from a datasnapshot in flutter?How to loop through a Firebase datasnapshot sub childrens? FlutterFlutter: The method forEach isn't defined for the class DataSnapshotFlutter Nested JSON ParsingFlutter get data from firebase without data key issues










0















I use flutter with package firebase_database. With the code



final FirebaseDatabase _database = FirebaseDatabase.instance;

@override
void initState()
super.initState();
_newsList = new List();

_newsQuery = _database
.reference()
.child('news')
.orderByChild('published')
.limitToFirst(10);

_newsQuery.onChildAdded.listen(_onEntryAdded);


_onEntryAdded(Event event)
setState(()
News n = News.fromSnapshot(event.snapshot);
_newsList.add(n);
);



i get a perfect list _newsList of all queried items. The news class is



 import 'package:firebase_database/firebase_database.dart';

class News
String key;
String image;
String text;
String title;
String published;

News(this.image, this.text, this.published);

News.fromSnapshot(DataSnapshot snapshot) :
key = snapshot.key,
text = snapshot.value["text"],
title = snapshot.value["title"],
image = snapshot.value["image"],
published = snapshot.value["published"];

toJson()
return
"image": image,
"text": text,
"title": title,
"published": published,
;




The json-structure in the database is:



database
|__news
|__post1
| |__text: "Lorem ipsum"
| |__title: "Title of post"
|
|__post2
|__ ...


Now i want to load a nested json-structure from the database with



database
|__news
|__category1
| |
| |__post1
| | |__text: "Lorem ipsum 1"
| | |__title: "Title of post1"
| |__post2
| | |__text: "Lorem ipsum 2"
| | |__title: "Title of post2"
| |__description: "description text"
| |__id: "id of category"
| .
| .
|
|__category2
| |
| |__post34
| | |__text: "Lorem ipsum 34"
| | |__title: "Title of post34"
| .
| .


I try to find a solution to load the nested DataSnapshots into class, but i always get exceptions. The best code i tried so far is



 class News {
final List<Category> categories;

News(this.categories);

factory News.fromSnapshot(DataSnapshot snapshot)

List<dynamic> listS = snapshot.value;

listS.forEach((value) =>
print('V $value')
);

List<Category> list = listS.map((i) => Category.fromJson(i)).toList();

return News(
categories: list
);




But this throws the exception



E/flutter ( 5882): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type '_InternalLinkedHashMap' is not a subtype of type 'Map'
E/flutter ( 5882): #0 new News.fromSnapshot. (package:app/models/news.dart:23:55)
E/flutter ( 5882): #1 MappedListIterable.elementAt (dart:_internal/iterable.dart:414:29)
E/flutter ( 5882): #2 ListIterable.toList (dart:_internal/iterable.dart:219:19)



I found in flutter and dart no code-example to load nested json with DataSnapshot. Do you know any code-sample?



If you want to see my full code, then look at https://github.com/matthiaw/gbh_app. The not working part is the nested json in calendar at https://github.com/matthiaw/gbh_app/blob/4de0f20f6162801db86ef6644609829c27a4dd76/lib/models/calendar.dart










share|improve this question
























  • Hi All, since this post i found no answer and get no answer. So i decided to change the datamodel to a "flat" json, so i can iterate and load without the nesting. Is not a good solution, but a working one. Any solution anyway appreciated.

    – Matthias Wegner
    Mar 11 at 18:39
















0















I use flutter with package firebase_database. With the code



final FirebaseDatabase _database = FirebaseDatabase.instance;

@override
void initState()
super.initState();
_newsList = new List();

_newsQuery = _database
.reference()
.child('news')
.orderByChild('published')
.limitToFirst(10);

_newsQuery.onChildAdded.listen(_onEntryAdded);


_onEntryAdded(Event event)
setState(()
News n = News.fromSnapshot(event.snapshot);
_newsList.add(n);
);



i get a perfect list _newsList of all queried items. The news class is



 import 'package:firebase_database/firebase_database.dart';

class News
String key;
String image;
String text;
String title;
String published;

News(this.image, this.text, this.published);

News.fromSnapshot(DataSnapshot snapshot) :
key = snapshot.key,
text = snapshot.value["text"],
title = snapshot.value["title"],
image = snapshot.value["image"],
published = snapshot.value["published"];

toJson()
return
"image": image,
"text": text,
"title": title,
"published": published,
;




The json-structure in the database is:



database
|__news
|__post1
| |__text: "Lorem ipsum"
| |__title: "Title of post"
|
|__post2
|__ ...


Now i want to load a nested json-structure from the database with



database
|__news
|__category1
| |
| |__post1
| | |__text: "Lorem ipsum 1"
| | |__title: "Title of post1"
| |__post2
| | |__text: "Lorem ipsum 2"
| | |__title: "Title of post2"
| |__description: "description text"
| |__id: "id of category"
| .
| .
|
|__category2
| |
| |__post34
| | |__text: "Lorem ipsum 34"
| | |__title: "Title of post34"
| .
| .


I try to find a solution to load the nested DataSnapshots into class, but i always get exceptions. The best code i tried so far is



 class News {
final List<Category> categories;

News(this.categories);

factory News.fromSnapshot(DataSnapshot snapshot)

List<dynamic> listS = snapshot.value;

listS.forEach((value) =>
print('V $value')
);

List<Category> list = listS.map((i) => Category.fromJson(i)).toList();

return News(
categories: list
);




But this throws the exception



E/flutter ( 5882): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type '_InternalLinkedHashMap' is not a subtype of type 'Map'
E/flutter ( 5882): #0 new News.fromSnapshot. (package:app/models/news.dart:23:55)
E/flutter ( 5882): #1 MappedListIterable.elementAt (dart:_internal/iterable.dart:414:29)
E/flutter ( 5882): #2 ListIterable.toList (dart:_internal/iterable.dart:219:19)



I found in flutter and dart no code-example to load nested json with DataSnapshot. Do you know any code-sample?



If you want to see my full code, then look at https://github.com/matthiaw/gbh_app. The not working part is the nested json in calendar at https://github.com/matthiaw/gbh_app/blob/4de0f20f6162801db86ef6644609829c27a4dd76/lib/models/calendar.dart










share|improve this question
























  • Hi All, since this post i found no answer and get no answer. So i decided to change the datamodel to a "flat" json, so i can iterate and load without the nesting. Is not a good solution, but a working one. Any solution anyway appreciated.

    – Matthias Wegner
    Mar 11 at 18:39














0












0








0








I use flutter with package firebase_database. With the code



final FirebaseDatabase _database = FirebaseDatabase.instance;

@override
void initState()
super.initState();
_newsList = new List();

_newsQuery = _database
.reference()
.child('news')
.orderByChild('published')
.limitToFirst(10);

_newsQuery.onChildAdded.listen(_onEntryAdded);


_onEntryAdded(Event event)
setState(()
News n = News.fromSnapshot(event.snapshot);
_newsList.add(n);
);



i get a perfect list _newsList of all queried items. The news class is



 import 'package:firebase_database/firebase_database.dart';

class News
String key;
String image;
String text;
String title;
String published;

News(this.image, this.text, this.published);

News.fromSnapshot(DataSnapshot snapshot) :
key = snapshot.key,
text = snapshot.value["text"],
title = snapshot.value["title"],
image = snapshot.value["image"],
published = snapshot.value["published"];

toJson()
return
"image": image,
"text": text,
"title": title,
"published": published,
;




The json-structure in the database is:



database
|__news
|__post1
| |__text: "Lorem ipsum"
| |__title: "Title of post"
|
|__post2
|__ ...


Now i want to load a nested json-structure from the database with



database
|__news
|__category1
| |
| |__post1
| | |__text: "Lorem ipsum 1"
| | |__title: "Title of post1"
| |__post2
| | |__text: "Lorem ipsum 2"
| | |__title: "Title of post2"
| |__description: "description text"
| |__id: "id of category"
| .
| .
|
|__category2
| |
| |__post34
| | |__text: "Lorem ipsum 34"
| | |__title: "Title of post34"
| .
| .


I try to find a solution to load the nested DataSnapshots into class, but i always get exceptions. The best code i tried so far is



 class News {
final List<Category> categories;

News(this.categories);

factory News.fromSnapshot(DataSnapshot snapshot)

List<dynamic> listS = snapshot.value;

listS.forEach((value) =>
print('V $value')
);

List<Category> list = listS.map((i) => Category.fromJson(i)).toList();

return News(
categories: list
);




But this throws the exception



E/flutter ( 5882): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type '_InternalLinkedHashMap' is not a subtype of type 'Map'
E/flutter ( 5882): #0 new News.fromSnapshot. (package:app/models/news.dart:23:55)
E/flutter ( 5882): #1 MappedListIterable.elementAt (dart:_internal/iterable.dart:414:29)
E/flutter ( 5882): #2 ListIterable.toList (dart:_internal/iterable.dart:219:19)



I found in flutter and dart no code-example to load nested json with DataSnapshot. Do you know any code-sample?



If you want to see my full code, then look at https://github.com/matthiaw/gbh_app. The not working part is the nested json in calendar at https://github.com/matthiaw/gbh_app/blob/4de0f20f6162801db86ef6644609829c27a4dd76/lib/models/calendar.dart










share|improve this question
















I use flutter with package firebase_database. With the code



final FirebaseDatabase _database = FirebaseDatabase.instance;

@override
void initState()
super.initState();
_newsList = new List();

_newsQuery = _database
.reference()
.child('news')
.orderByChild('published')
.limitToFirst(10);

_newsQuery.onChildAdded.listen(_onEntryAdded);


_onEntryAdded(Event event)
setState(()
News n = News.fromSnapshot(event.snapshot);
_newsList.add(n);
);



i get a perfect list _newsList of all queried items. The news class is



 import 'package:firebase_database/firebase_database.dart';

class News
String key;
String image;
String text;
String title;
String published;

News(this.image, this.text, this.published);

News.fromSnapshot(DataSnapshot snapshot) :
key = snapshot.key,
text = snapshot.value["text"],
title = snapshot.value["title"],
image = snapshot.value["image"],
published = snapshot.value["published"];

toJson()
return
"image": image,
"text": text,
"title": title,
"published": published,
;




The json-structure in the database is:



database
|__news
|__post1
| |__text: "Lorem ipsum"
| |__title: "Title of post"
|
|__post2
|__ ...


Now i want to load a nested json-structure from the database with



database
|__news
|__category1
| |
| |__post1
| | |__text: "Lorem ipsum 1"
| | |__title: "Title of post1"
| |__post2
| | |__text: "Lorem ipsum 2"
| | |__title: "Title of post2"
| |__description: "description text"
| |__id: "id of category"
| .
| .
|
|__category2
| |
| |__post34
| | |__text: "Lorem ipsum 34"
| | |__title: "Title of post34"
| .
| .


I try to find a solution to load the nested DataSnapshots into class, but i always get exceptions. The best code i tried so far is



 class News {
final List<Category> categories;

News(this.categories);

factory News.fromSnapshot(DataSnapshot snapshot)

List<dynamic> listS = snapshot.value;

listS.forEach((value) =>
print('V $value')
);

List<Category> list = listS.map((i) => Category.fromJson(i)).toList();

return News(
categories: list
);




But this throws the exception



E/flutter ( 5882): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type '_InternalLinkedHashMap' is not a subtype of type 'Map'
E/flutter ( 5882): #0 new News.fromSnapshot. (package:app/models/news.dart:23:55)
E/flutter ( 5882): #1 MappedListIterable.elementAt (dart:_internal/iterable.dart:414:29)
E/flutter ( 5882): #2 ListIterable.toList (dart:_internal/iterable.dart:219:19)



I found in flutter and dart no code-example to load nested json with DataSnapshot. Do you know any code-sample?



If you want to see my full code, then look at https://github.com/matthiaw/gbh_app. The not working part is the nested json in calendar at https://github.com/matthiaw/gbh_app/blob/4de0f20f6162801db86ef6644609829c27a4dd76/lib/models/calendar.dart







json firebase-realtime-database dart flutter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 11 at 18:43







Matthias Wegner

















asked Mar 6 at 22:04









Matthias WegnerMatthias Wegner

3618




3618












  • Hi All, since this post i found no answer and get no answer. So i decided to change the datamodel to a "flat" json, so i can iterate and load without the nesting. Is not a good solution, but a working one. Any solution anyway appreciated.

    – Matthias Wegner
    Mar 11 at 18:39


















  • Hi All, since this post i found no answer and get no answer. So i decided to change the datamodel to a "flat" json, so i can iterate and load without the nesting. Is not a good solution, but a working one. Any solution anyway appreciated.

    – Matthias Wegner
    Mar 11 at 18:39

















Hi All, since this post i found no answer and get no answer. So i decided to change the datamodel to a "flat" json, so i can iterate and load without the nesting. Is not a good solution, but a working one. Any solution anyway appreciated.

– Matthias Wegner
Mar 11 at 18:39






Hi All, since this post i found no answer and get no answer. So i decided to change the datamodel to a "flat" json, so i can iterate and load without the nesting. Is not a good solution, but a working one. Any solution anyway appreciated.

– Matthias Wegner
Mar 11 at 18:39













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032892%2fget-with-flutter-a-nested-json-from-firebase-datasnapshot%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032892%2fget-with-flutter-a-nested-json-from-firebase-datasnapshot%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко