我试图以Json的身份从我的数据库(MYSQL DATABASE)中添加数据,但出现此错误可以帮助我,因为我正在寻找解决问题的方法,但找不到它
import 'package:flutter/material.dart';
import 'package:youtubeclone/pages/class/categores.dart';
import 'package:youtubeclone/pages/componets/appbarallproject.dart';
import 'package:youtubeclone/pages/componets/mydrawer.dart';
import 'dart:io';
import 'package:image_picker/image_picker.dart';
import '../class/dropdownmenuclass.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Upload extends StatefulWidget {
@override
_UploadState createState() => _UploadState();
}
class _UploadState extends State<Upload> {
GlobalKey<ScaffoldState> _drawerKey = GlobalKey();
CategoresList _selectedCategory2;
List<CategoresList> listcategory = <CategoresList>[];
Future<List<CategoresList>> getCategories() async {
var url = "http://10.0.2.2/videoTube/chosecategories.php";
var response = await http.get(url);
var responsebody = jsonDecode(response.body);
for (int i = 0; i < responsebody.length; i++) {
listcategory.add(responsebody[i]['name']);
}
}
@override
void initState() {
getCategories();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _drawerKey,
drawer: MyDrawer(),
appBar: AppBarAllProject(drawerKey: _drawerKey),
body: Container(
child: Center(
child: AnimatedContainer(
duration: Duration(milliseconds: 600),
curve: Curves.easeOutBack,
width: MediaQuery.of(context).size.width - 40,
height: 1000,
child: Card(
elevation: 5.0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
DropdownButton<CategoresList>(
hint: Text("Select Categore"),
value: _selectedCategory2,
onChanged: (CategoresList catey) {
setState(() {
_selectedCategory2 = catey;
});
},
items: listcategory?.map((CategoresList categoreslist) {
return DropdownMenuItem<CategoresList>(
value: categoreslist,
child: Row(
children: <Widget>[
Text(
categoreslist.name,
),
],
),
);
})?.toList() ??
[],
),
],
),
),
),
),
),
);
}
}
这是我遇到错误的函数,我得到了错误,具体在行listcategory.add(responsebody [i] ['name'])
Future<List<CategoresList>> getCategories() async {
var url = "http://10.0.2.2/videoTube/chosecategories.php";
var response = await http.get(url);
var responsebody = jsonDecode(response.body);
for (int i = 0; i < responsebody.length; i++) {
listcategory.add(responsebody[i]['name']);
}
}
此类CategoresList
import 'package:flutter/material.dart';
class CategoresList {
final name;
CategoresList({this.name});
}
答案 0 :(得分:1)
错误消息不言自明。您正在尝试向包含CategoresList类型的列表中添加字符串。
从responsebody [i] ['name']创建一个新的CategoresList,然后将其添加到listcategory.add();
var name = responsebody[i]['name'];
var category = CategoresList(name: name);
listcategory.add(category);