飘动-在导航期间希望文本变量返回null(希望在提升按钮期间优先考虑导航中的函数)



我正在发送一个图像到API,我想显示Snackbar显示如果图像上传与否。
我在类中定义了文本变量。然后从API响应中我改变了那个变量但是在提升按钮导航期间文本的值被改变了但是它显示的是之前被分配的

这是我的f

Future CreateAuditAPI(String userId, String warehouseId, String base64IMG, String description) async{
final Uri uri = Uri.parse("https://firebase.google.com/" );/*sample url for getting response I know it wil not work*/

CreateAudit obj = CreateAudit(
userID: userId,
warehouseID: warehouseId,
description: [description],
imagedata: [base64IMG]
);
var d = jsonEncode(obj);
print("printing data before passing");
print(d);
try {
print("printing in try");
final http.Response response = await client.post(uri,
headers: headers, body: d);

Map<String, dynamic> jsonResponse = jsonDecode(response.body);
if (jsonResponse["status"] == 200) {
text = jsonResponse["message"];
return jsonResponse["data"];


} else {
text = "Something Went Wrong";
throw Exception(jsonResponse["msg"]);
}
} catch(e) {
print(e);


}


}

而Snackbar的显示在这里

class DisplayScreen extends StatefulWidget {
/*DisplayScreen({super.key, required this.imagePath});*/
String imagePath;
String warehouseid ;
DisplayScreen(this.imagePath, this.warehouseid);

@override
State<DisplayScreen> createState() => _DisplayScreenState();
}

class _DisplayScreenState extends State<DisplayScreen> {
String? dropdownBuldingtype = '';

String? _imagData;
String? userID;
String? text='Uploaded Successfully';


Future<String> getExifFromFile() async {
if (File(widget.imagePath) == null) {
return "";
}

final fileBytes = File(widget.imagePath)!.readAsBytesSync();
final data = await readExifFromBytes(fileBytes);
if (data.isEmpty) {
print("No EXIF information found");
return "";
}
final datetime = data['EXIF DateTimeOriginal']?.toString();
if (datetime == null) {
print("datetime information not found");
return "";
}
String? datetime1;

datetime1 = datetime;

print(widget.imagePath);
File imagefile = File(widget.imagePath); //convert Path to File
Uint8List imagebytes = await imagefile.readAsBytes(); //convert to bytes
_imagData = base64.encode(imagebytes); //convert bytes to base64 string




return datetime1;
}
Future CreateAuditAPI(String userId, String warehouseId, String base64IMG, String description) async{

final Uri uri = Uri.parse("https://firebase.google.com/" );
print('insidewarehouse');
print(uri);
CreateAudit obj = CreateAudit(
userID: userId,
warehouseID: warehouseId,
description: [description],
imagedata: [base64IMG]
);
var d = jsonEncode(obj);
print("printing data before passing");
print(d);
try {
print("printing in try");
final http.Response response = await client.post(uri,
headers: headers, body: d);

Map<String, dynamic> jsonResponse = jsonDecode(response.body);
if (jsonResponse["status"] == 200) {
text = jsonResponse["message"];
return jsonResponse["data"];

} else {
text = 'Something Went Wrong';
throw Exception(jsonResponse["msg"]);
}
} catch(e) {
print(e);

}


}
void getuserid() async{
SharedPreferences _prefs = await SharedPreferences.getInstance();
userID =  _prefs.getString("userid");
}
TextEditingController description = TextEditingController();

final client = http.Client();
final headers ={
'Content-type': 'application/json'
};
/*var data;*/
@override
void initState() {
getuserid();
// TODO: implement initState
super.initState();
}
@override
Widget build(BuildContext context) {
print("attal======>"+widget.imagePath);
/*data = ModalRoute.of(context)!.settings.arguments;
// dropdownBuldingtype = data["buidlingtype"];
print(data);*/
getExifFromFile();
getuserid();
print("atal");
print(widget.imagePath);
/*String? warehouseID= data["warehouseid"].toString();
print(warehouseID);*/
final size = MediaQuery
.of(context)
.size;
final String _description = description.text;
String? warehouseID = widget.warehouseid;
print('warehiii');
print(warehouseID);
print('useriiiiiii');
print(userID);


return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
leading: IconButton(icon: const Icon(Icons.close, color: Colors.red, size: 50,), padding: EdgeInsets.all(4.0), onPressed: () {Navigator.pop(context); },),
actions:  [IconButton(icon: const Icon(Icons.check, color: Colors.green, size: 50,), padding: EdgeInsets.only(right: 4.0),onPressed: () {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
actions: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
IconButton(onPressed: (){
Navigator.of(ctx).pop();
}, icon: Icon(Icons.close, color: Colors.red,) )
],
),
Padding(padding:  EdgeInsets.symmetric(horizontal: size.width*0.07),
child: Text('Description : ', style: TextStyle(fontWeight: FontWeight.bold),),
),
SizedBox(height: 20,),
Padding(
padding:  EdgeInsets.symmetric(horizontal: size.width*0.07),
child: TextFormField(
controller: description,
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(borderSide: BorderSide(color: Colors.black, width: 2)),
enabledBorder:  OutlineInputBorder(borderSide: BorderSide(color: Colors.grey, width: 1 ),),
),

),
),
SizedBox(height: size.height*0.05,),
Padding(
padding: EdgeInsets.symmetric(horizontal: size.width*0.07),
child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(onPressed: (){}, child: Text('More Image'),style: ElevatedButton.styleFrom(
primary: Colors.orange.withOpacity(0.8),
),),
ElevatedButton(onPressed: (){CreateAuditAPI(userID!,warehouseID,_imagData!,description.text);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("$text"),action: SnackBarAction(label: 'OK',onPressed: (){
Navigator.pop(context);
},),));
Navigator.pop(context);



},

child: Text('Upload'), style: ElevatedButton.styleFrom(
primary: Colors.orange.withOpacity(0.8),

),)
],
),
)
],
),
],
),
);},)],
),
backgroundColor: Colors.black,
/*appBar: AppBar(title: const Text('Display the Picture')),*/
// The image is stored as a file on the device. Use the `Image.file`
// constructor with the given path to display the image.
body: SizedBox.expand(child: Image.file(File(widget.imagePath),

)),



);
}

}

这就是问题.......

ElevatedButton(onPressed: (){CreateAuditAPI(userID!,warehouseID,_imagData!,description.text);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("$text"),action: SnackBarAction(label: 'OK',onPressed: (){
Navigator.pop(context);
},),));
Navigator.pop(context);


},

child: Text('Upload'), style: ElevatedButton.styleFrom(
primary: Colors.orange.withOpacity(0.8),

),)

它在控制台上调用API,但在Snackbar中,它没有将新值分配给文本变量。升高的按钮是调用创建审计的功能,但它做创建审计和显示零食栏在同一时间,不给文本的更新值给零食栏。

我已经尝试从升高的按钮中删除Snackbar,并将Snackbar放在CreateAuditAPI函数中,但它没有调用Snackbar。

我猜问题是没有等待请求作为同步处理您应该等待API请求

ElevatedButton(onPressed: () async {
await CreateAuditAPI(userID!,warehouseID,_imagData!,description.text);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content:Text("$text"),action: SnackBarAction(label: 'OK',onPressed: (){  Navigator.pop(context);
},),));
Navigator.pop(context);
}, 

相关内容

最新更新