单击“提交”按钮时,我的终端出现此错误:
I/flutter (13328): Error NoSuchMethodError: The method 'openRead' was called on null.
I/flutter (13328): Receiver: null
I/flutter (13328): Tried calling: openRead()
这是我的editAttraction.dart
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:myapp/model/api.dart';
import 'package:myapp/model/attractionModel.dart';
import 'package:http/http.dart' as http;
import 'package:async/async.dart';
import 'package:path/path.dart' as path;
import 'package:shared_preferences/shared_preferences.dart';
class EditAttraction extends StatefulWidget {
final AttractionModel model;
final VoidCallback reload;
EditAttraction(this.model, this.reload);
@override
_EditAttractionState createState() => _EditAttractionState();
}
class _EditAttractionState extends State<EditAttraction> {
final _key = new GlobalKey<FormState>();
String attractionName, description, price, idUsers;
File _imageFile;
_pilihGallery() async{
var image = await ImagePicker.pickImage(
source: ImageSource.gallery,
maxHeight: 1920.0,
maxWidth: 1080);
setState(() {
_imageFile = image;
});
}
TextEditingController txtAttractionName, txtDescription, txtPrice;
setup() async{
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
idUsers = preferences.getString("id");
});
txtAttractionName = TextEditingController(text: widget.model.attractionName);
txtDescription = TextEditingController(text: widget.model.description);
txtPrice = TextEditingController(text: widget.model.price);
}
check(){
final form = _key.currentState;
if (form.validate()) {
form.save();
submit();
} else {
}
}
submit() async {
try {
var stream = http.ByteStream(DelegatingStream.typed(_imageFile.openRead()));
var length = await _imageFile.length();
var uri = Uri.parse(BaseUrl.editAttraction);
var request = http.MultipartRequest("POST",uri);
request.fields['attractionName'] = attractionName;
request.fields['description'] = description;
request.fields['price'] = price;
request.fields['idUsers'] = idUsers;
request.fields['idAttraction'] = widget.model.id;
request.files.add(http.MultipartFile("image", stream, length, filename: path.basename(_imageFile.path)));
var response = await request.send();
if (response.statusCode > 2) {
print("image uploaded");
setState(() {
widget.reload();
Navigator.pop(context);
});
} else {
print("image failed to upload");
}
} catch (e) {
debugPrint("Error $e");
}
// final response = await http.post(BaseUrl.editAttraction, body : {
// "attractionName" : attractionName,
// "description" : description,
// "price" : price,
// "idAttraction" : widget.model.id
// });
// final data = jsonDecode(response.body);
// int value = data['value'];
// String message = data['message'];
// if (value == 1) {
// setState(() {
// widget.reload();
// Navigator.pop(context);
// });
// } else {
// print(message);
// }
}
@override
void initState() {
// TODO: implement initState
super.initState();
setup();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Form(
key: _key,
child: ListView(
padding: EdgeInsets.all(16.0),
children: <Widget>[
Container(
width: double.infinity,
height: 150.0,
child: InkWell(
onTap:(){
_pilihGallery();
},
child: _imageFile == null
? Image.network('http://192.168.42.48/myapp/upload/' + widget.model.image)
: Image.file(_imageFile,
fit: BoxFit.fill,
),
),
),
TextFormField(
controller: txtAttractionName,
onSaved: (e) => attractionName = e,
decoration: InputDecoration(
labelText: "Places Name"
),
),
TextFormField(
controller: txtDescription,
onSaved: (e) => description = e,
decoration: InputDecoration(
labelText: "Description"
),
),
TextFormField(
controller: txtPrice,
onSaved: (e) => price = e,
decoration: InputDecoration(
labelText: "Price"
),
),
MaterialButton(
onPressed: (){
check();
}, child: Container(
padding: EdgeInsets.all(13),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [
Colors.pink,
Colors.purple,
]
)
),
child: Text("Submit", style: TextStyle(color: Colors.white),),
),
)
],
),
),
);
}
}
这是我的editAttraction.php
<?php
require "../config/connect.php";
if ($_SERVER['REQUEST_METHOD']=="POST")
{
#code...
$response = array();
$attractionName = $_POST['attractionName'];
$description = $_POST['description'];
$price = $_POST['price'];
$idAttraction = $_POST['idAttraction'];
$image = date('dmTHis').str_replace(" ","",basename($_FILES['image']['name']));
$imagePath = "../upload/".$image;
move_uploaded_file($_FILES['image']['tmp_name'], $imagePath);
$insert = "UPDATE attraction SET attractionName = '$attractionName', description = '$description', price = '$price', image = '$image' WHERE id = '$idAttraction'";
if (mysqli_query($con, $insert))
{
# code...
$response['value']=1;
$response['message']="Added!";
echo json_encode($response);
}
else
{
# code...
$response['value']=0;
$response['message']="Failed to added";
echo json_encode($response);
}
}
?>
这是我的deleteAttraction.php,我不知道这是否与错误有关
<?php
require "../config/connect.php";
if ($_SERVER['REQUEST_METHOD']=="POST")
{
#code...
$response = array();
$idAttraction = $_POST['idAttraction'];
$insert = "DELETE FROM attraction WHERE id = '$idAttraction' ";
if (mysqli_query($con, $insert))
{
# code...
$response['value']=1;
$response['message']="Deleted!";
echo json_encode($response);
}
else
{
# code...
$response['value']=0;
$response['message']="Failed to delete";
echo json_encode($response);
}
}
?>
在此之前(大约2周前),我运行该应用程序,并尝试更新和删除,并且效果很好。但是,现在我再次尝试,更新在终端中出现错误,并且我无法删除应用程序中的任何项目,并且显示消息“无法删除”。
已编辑: 我可以对其进行更新,但必须从上传新图像进行更新,然后才能提交所有内容。
我在编码中写错了吗?我真的需要帮助。