1

I have a list of string from rest API like this

response

I try to split it using this code e.review!.map((e) => e).join(" "),

and I get this result current result

but my expectation result is

expected result

How do I do that? Thank you

model.dart

  RatingDataModel({
    this.rating,
    this.review,
  });

  int? rating;
  List<String>? review;

  factory RatingDataModel.fromJson(Map<String, dynamic> json) =>
      RatingDataModel(
        rating: json["rating"],
        //separate the review by space

        review: json["review"] != null
            ? List<String>.from(json["review"].map((x) => x))
            : null,
      );

  Map<String, dynamic> toJson() => {
        "rating": rating,
        "review": List<dynamic>.from(
          review!.map(
            (x) => x,
          ),
        ),
      };
}
3
  • Just iterate through the array and for each element call the split() function Commented Sep 23, 2022 at 2:45
  • is it in the model? Commented Sep 23, 2022 at 2:58
  • how do you show the result? the problem is not in parsing it, is in showing it. Commented Sep 23, 2022 at 5:13

4 Answers 4

2

You need to loop through since it is a list.

enter image description here

Here is the full working code as a Tags widget

import 'package:flutter/material.dart';

class Tags extends StatelessWidget {
  const Tags({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Tag Demo'),
      ),
      body: Wrap(
        spacing: 8.0, // gap between adjacent chips
        runSpacing: 4.0, // gap between lines
        children: getChips(),
      ),
    );
  }

  List<Chip> getChips() {
    List<String> review = [
      "Tidak ramah",
      "Tidak sesuai dengan foto",
      "Tidak sesuai deskripsi",
      "Tailor terlambat",
      "Lokasi tempt tidak sesuai alamat",
      "Tempat tidak ditemukan",
      "Tailor kasar",
      "Tailor melakukan kekerasa / pelecehan",
      "Tempat tidak nyaman"
    ];

    List<Chip> tags = [];
    review.asMap().forEach((index, singleReview) {
      tags.add(
        Chip(
          avatar: CircleAvatar(
            child: Text((index + 1).toString()),
          ),
          label: Text(singleReview),
        ),
      );
    });

    return tags;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome! Wrap (api.flutter.dev/flutter/widgets/Wrap-class.html) has spacing options and I have added in the code.
1

Try the following code:

ListView.builder(
  itemCount: e.review!.length,
  itemBuilder: (context, index) {
    final review = e.review![index]; // Use this

    return ......;
  },
);

Comments

1

Use ListView.builder instead of ListView.

 @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: e.review!.length,
        itemBuilder: (context, index) {
          return ChoiceChip(
                   ...
            label: Text(e.review![index]));
        });
  }

Comments

0

You don't need to split since it's a list of strings,just iterate through with a 'listview'

 e.map((e) => e).toList()

1 Comment

imgur.com/MsguKpb the result still the same

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.