[Solved] How to split a string from rest api?

Question

Asked by Muhammad Y on September 23, 2022 (source).

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,
          ),
        ),
      };
}

Answer

Question answered by Myo W (source).

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;
  }
}
DART FLUTTER
SHARE: