0

I'm trying to make a page for each letter of the alphabet. I need to take the data from a JSON. So I don't have to define a class for each letter.

        Row(
          children: <Widget>[
            Expanded(
              child: Stack(
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.all(sizeY / 3 - 1),
                    height: sizeY,
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image: AssetImage(
                                "assets/square-" + imgColor + "-big.png"),
                            fit: BoxFit.fitHeight)),
                    child: Padding(
                      padding: const EdgeInsets.all(15.0),
                      child: Center(
                        child: Text(
                          this.letter + this.letter.toLowerCase(),
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 40.0,
                              fontFamily: 'ConcertOne',
                              fontWeight: FontWeight.normal),
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: Text(
                this.shembulli1,
                style: TextStyle(
                    color: Colors.pink,
                    fontWeight: FontWeight.bold,
                    fontSize: 30.0),
                textAlign: TextAlign.center,
              ),
            ),
            Expanded(
                child: Container(
              width: sizeY / 2,
              height: sizeX / 2,
              decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage("assets/" + shembulliImg1 + ".png"),
                      fit: BoxFit.contain)),
            ))
          ],
        ),

Where shembulli1, shembulli2, shembulliImg1, shembulliImg2, letter, and imgColor have to come from JSON which looks something like this:

    {
        "shembulli1":"Bleta",
        "shembulli2":"Biçikleta",
        "shembulliImg1":"bee",
        "shembulliImg2":"bike",
        "letter":"B",
        "imgColor":"blue"
    },
    {
        "shembulli1":"Cicërima",
        "shembulli2":"Certifikata",
        "shembulliImg1":"bird",
        "shembulliImg2":"letter",
        "letter":"C",
        "imgColor":"yellow"
    },
2
  • yes you can return json data into anything. Commented Oct 22, 2020 at 8:40
  • All flutter tutorials only show how to return the data from json in a ListView, do you have any idea/example on how I could parse the data properly in my case? Commented Oct 22, 2020 at 8:51

2 Answers 2

1

Fetch JSON data to make a List. from that List you can do

yourList.forEach((post) {
    //generate widget for each element
})
void getPostsData() {
  List<dynamic> AlphabetList = AlphabetModel;

  /*    
        List defined as the JSON
        if you have a JSON in your PC then import it
        if your're fetching data from server like firebase then you've to fetch data something like this

        fetchData() {
            //'data' is the table from where your JSON is generating
            CollectionReference collectionReference = Firestore.instance.collection('data');    
            collectionReference.snapshots().listen((snapshot) {

                setState(() {
                    //this will give JSON as a List
                    AlphabetList = snapshot.documents;
                });
            });
        } 
    */


  AlphabetList.forEach((post) {
    Row(
      children: <Widget>[
        Expanded(
          child: Stack(
            children: <Widget>[
              Container(
                margin: EdgeInsets.all(sizeY / 3 - 1),
                height: sizeY,
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage(
                            "assets/square-" + post["imgColor"] + "-big.png"),
                        fit: BoxFit.fitHeight)),
                child: Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: Center(
                    child: Text(
                      post["letter"] + post["letter"].toLowerCase(),
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 40.0,
                          fontFamily: 'ConcertOne',
                          fontWeight: FontWeight.normal),
                      textAlign: TextAlign.center,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
        Expanded(
          child: Text(
            post["shembulli1"],
            style: TextStyle(
                color: Colors.pink,
                fontWeight: FontWeight.bold,
                fontSize: 30.0),
            textAlign: TextAlign.center,
          ),
        ),
        Expanded(
            child: Container(
              width: sizeY / 2,
              height: sizeX / 2,
              decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage("assets/" + post["shembulliImg1"] + ".png"),
                      fit: BoxFit.contain)),
            ))
      ],
    )
    ,
  })
}
Sign up to request clarification or add additional context in comments.

2 Comments

This looks good, the json is from a local file "assets/data/alfabeti.json" , could you include the fetchData and setState for this?
youtube.com/watch?v=Cn6VCTaHB-k&t=531s In this he got a JSON and made a const List variable from it. then he saved that List in a dart file. and imported that dart file. After that just assign the const List to the AlphabetList.
0

You can use this model to work with.

      
class AlphabetModel {
  String shembulli1;
  String shembulli2;
  String shembulliImg1;
  String shembulliImg2;
  String letter;
  String imgColor;

  AlphabetModel(
      {this.shembulli1,
      this.shembulli2,
      this.shembulliImg1,
      this.shembulliImg2,
      this.letter,
      this.imgColor});

  AlphabetModel.fromJson(Map<String, dynamic> json) {
    shembulli1 = json['shembulli1'];
    shembulli2 = json['shembulli2'];
    shembulliImg1 = json['shembulliImg1'];
    shembulliImg2 = json['shembulliImg2'];
    letter = json['letter'];
    imgColor = json['imgColor'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['shembulli1'] = this.shembulli1;
    data['shembulli2'] = this.shembulli2;
    data['shembulliImg1'] = this.shembulliImg1;
    data['shembulliImg2'] = this.shembulliImg2;
    data['letter'] = this.letter;
    data['imgColor'] = this.imgColor;
    return data;
  }
}

Now it becomes easier to define a serialized list from your JSON. After that, you can build a simple ListView. Look here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.