[英]Expected a value of type 'String', but got one of type 'Null' while data updation
我在 MySQL 到 flutter HTTp API 中創建了 CRUD 操作。我可以從 mysql 數據庫中獲取數據。 我想編輯/更新其中一項。 如果我 select 任何一項,它將顯示一個黑色字段,在 initstate() 添加和最終變量聲明后顯示錯誤。 我很迷惑。
這是 main.dart 獲取詳細信息:
ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => new updateUsersData(
id: list[i]["ID"],
name: list[i]["NAME"],
mobile: list[i]["MOBILE"])));
},
title: Text(list[i]["NAME"]),
subtitle: Text(list[i]['MOBILE']),
leading: CircleAvatar(
child: Text(
list[i]["NAME"].toString().substring(0, 1).toUpperCase()),
),
),
這是 dart 中的更新用戶代碼
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class updateUsersData extends StatefulWidget {
final String id;
final String name;
final String mobile;
const updateUsersData(
{Key? key, required this.id, required this.name, required this.mobile})
: super(key: key);
@override
_updateUsersDataState createState() => _updateUsersDataState();
}
class _updateUsersDataState extends State<updateUsersData> {
final txtName = TextEditingController();
final txtMobile = TextEditingController();
bool _valname = false;
bool _valmobile = false;
@override
void dispose() {
txtName.dispose();
txtMobile.dispose();
super.dispose();
}
@override
void initState() {
txtName.text = widget.name;
txtMobile.text = widget.mobile;
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Update User Data"),
),
body: Container(
padding: EdgeInsets.all(30.0),
child: Column(
children: [
TextField(
controller: txtName,
decoration: InputDecoration(
hintText: "Enter Your Name",
label: Text("Name"),
errorText: _valname ? "Name Cannot be Empty" : null),
),
TextField(
controller: txtMobile,
decoration: InputDecoration(
hintText: "Enter Your Mobile",
label: Text("Mobile"),
errorText: _valmobile ? "Mobile Cannt be Empty" : null),
),
ButtonBar(
children: [
ElevatedButton(
onPressed: () {
setState(() {
txtName.text.isEmpty ? _valname = true : _valname = false;
txtMobile.text.isEmpty
? _valmobile = true
: _valmobile = false;
if (_valname == false && _valmobile == false) {
//_savedetail(txtName.text, txtMobile.text);
}
});
},
child: Text("Update Data"),
style: ElevatedButton.styleFrom(primary: Colors.green),
),
ElevatedButton(
onPressed: () {
txtMobile.clear();
txtName.clear();
},
child: Text("Delete Data"),
style: ElevatedButton.styleFrom(
primary: Colors.red,
),
)
],
)
],
),
),
);
}
}
錯誤發生在聲明最終變量之后
請幫助我如何從獲取的數據中獲取選定的值
我可以看到您對class updateUsersData extends StatefulWidget
的實現需要 3 個類型為String
的必需屬性。 並在以下代碼中:
sersData(
id: list[i]["ID"],
name: list[i]["NAME"],
mobile: list[i]["MOBILE"])));
您正在使用ID
、 NAME
和MOBILE
鍵讀取索引i
處的值。
毫無疑問,您的問題出在這些鍵的一個或多個值中。 那些鍵的值不是字符串,它們中的一個或部分或全部!
如果我是你,我會在這一行中放置一個斷點:
const updateUsersData
... class 的構造函數,並檢查傳入值。
對您來說,另一點是 Dart 類需要是 Upper Camel Case,如此處所述,因此您最好將updateUsersData
class 重命名為UpdateUsersData
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.