#flutter #dart
Вопрос:
Я использую эту библиотеку для отображения текста с помощью api. chips_choice
Данные, которые я получаю от api, — это «A», «B». Но я хочу вставить новый текст «Все» DishMenuTypesDishTypesDishTypesLocales
во name
вложенный список. Результат, который я хочу отобразить после вставки текста «Все», будет «Все», «А», «В». Ниже приведена модель и код дротика, которые я пробовал до сих пор.
Модель
class DishMenuTypesDishTypesDishTypesLocales {
String? id;
String? name;
String? lang;
String? createdAt;
String? updatedAt;
String? dishTypeId;
DishMenuTypesDishTypesDishTypesLocales({
this.id,
this.name,
this.lang,
this.createdAt,
this.updatedAt,
this.dishTypeId,
});
DishMenuTypesDishTypesDishTypesLocales.fromJson(Map<String, dynamic> json) {
id = json["id"]?.toString();
name = json["name"]?.toString();
lang = json["lang"]?.toString();
createdAt = json["createdAt"]?.toString();
updatedAt = json["updatedAt"]?.toString();
dishTypeId = json["dishTypeId"]?.toString();
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data["id"] = id;
data["name"] = name;
data["lang"] = lang;
data["createdAt"] = createdAt;
data["updatedAt"] = updatedAt;
data["dishTypeId"] = dishTypeId;
return data;
}
}
class DishMenuTypesDishTypes {
String? id;
bool? isDeleted;
String? createdAt;
String? updatedAt;
List<DishMenuTypesDishTypesDishTypesLocales?>? dishTypesLocales;
DishMenuTypesDishTypes({
this.id,
this.isDeleted,
this.createdAt,
this.updatedAt,
this.dishTypesLocales,
});
DishMenuTypesDishTypes.fromJson(Map<String, dynamic> json) {
id = json["id"]?.toString();
isDeleted = json["is_deleted"];
createdAt = json["createdAt"]?.toString();
updatedAt = json["updatedAt"]?.toString();
if (json["dish_types_locales"] != null) {
final v = json["dish_types_locales"];
final arr0 = <DishMenuTypesDishTypesDishTypesLocales>[];
v.forEach((v) {
arr0.add(DishMenuTypesDishTypesDishTypesLocales.fromJson(v));
});
dishTypesLocales = arr0;
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data["id"] = id;
data["is_deleted"] = isDeleted;
data["createdAt"] = createdAt;
data["updatedAt"] = updatedAt;
if (dishTypesLocales != null) {
final v = dishTypesLocales;
final arr0 = [];
v!.forEach((v) {
arr0.add(v!.toJson());
});
data["dish_types_locales"] = arr0;
}
return data;
}
}
class DishMenuTypes {
String? message;
List<DishMenuTypesDishTypes?>? DishTypes;
DishMenuTypes({
this.message,
this.DishTypes,
});
DishMenuTypes.fromJson(Map<String, dynamic> json) {
message = json["message"]?.toString();
if (json["DishTypes"] != null) {
final v = json["DishTypes"];
final arr0 = <DishMenuTypesDishTypes>[];
v.forEach((v) {
arr0.add(DishMenuTypesDishTypes.fromJson(v));
});
DishTypes = arr0;
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data["message"] = message;
if (DishTypes != null) {
final v = DishTypes;
final arr0 = [];
v!.forEach((v) {
arr0.add(v!.toJson());
});
data["DishTypes"] = arr0;
}
return data;
}
}
Дротик
Widget buildDishMenuTypes(List<DishMenuTypesDishTypes> data) {
data.insert(
0, new DishMenuTypesDishTypes(dishTypesLocales: )); //Here trying to add "All"
return Container(
child:ChoiceChip(
label: Text(data[index].dishTypesLocales![0]!.name!.toString()), // The data i'm getting is "A", "B"
)
)
}