import 'package:flutter/material.dart';
import 'package:flutter_app/utils/c_audio_player.dart';
import 'package:flutter_app/utils/index.dart';
import 'package:flutter_app/widget/index.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'provider.dart';
import 'dart:io';
//import 'package:path/path.dart' as path;
class PackageInputController extends GetxController {
final _storage = GetStorage();
final provider = Get.put(PackageInputProvider());
final accountController = TextEditingController();
final packageNoController = TextEditingController();
final packagePositionController = TextEditingController();
final packageWeightController = TextEditingController();
final packageNumController = TextEditingController();
final imageUploaderController = ImageUploaderController();
@override
void onInit() {
super.onInit();
Future.delayed(Duration(milliseconds: 500), () {
packageNumController.text = "1";
var position = _storage.read('packagePosition') ?? '';
packagePositionController.text = position;
});
}
void onClear() {
packageNoController.clear();
packageWeightController.clear();
packageNumController.clear();
imageUploaderController.clear();
//accountController.clear();
}
Future<void> onSubmit() async {
var files = imageUploaderController.files
.where((e) => !e.isLast && e.file != null) // 过滤掉 'isLast' 项
.map((e) => e.file)
.toList();
final form = FormData({
'expressno': packageNoController.text,
'position': packagePositionController.text,
'goodcount': packageNumController.text,
'weight': packageWeightController.text,
'account': accountController.text,
'adminid': _storage.read('auth_info_userid'),
});
for (var e in files) {
if (e == null || e.path == null) {
Get.log("File does not exist.");
continue;
}
form.files.add(MapEntry<String, MultipartFile>(
'file', MultipartFile(e.path, filename: e.name)));
}
final map = await provider.addPackage(form);
map['msg'].toString().showSnackbar();
if (map['res'] != 1) {
AudioPlayerUtil().play(AudioPlayerUtil.SOUNDS_WARN);
return;
}
AudioPlayerUtil().play(AudioPlayerUtil.SOUNDS_SEND_SUCCESS);
_storage.write('packagePosition', packagePositionController.text);
_storage.save();
onClear();
}
}
Path和name 都有值但是不知道为什么只要有图片就不能提交到后台,接口用postman测试过了可以上传图片没问题,报错 出现异常。
_TypeError (type 'Null' is not a subtype of type 'String')` 一直显示这个什么null,但是我的form.file是有添加图片文件的啊,