Flutter 从颤振插入后,数据库中的数据重复

Flutter 从颤振插入后,数据库中的数据重复,flutter,Flutter,我尝试一次保存一组图像。这些图像已成功保存,但每个图像都有一个名称+路径。这些数据保存在MySQL数据库中,但问题是保存过程会重复多次。这意味着如果我保存两张图片,我会在数据库中发现这些图片已经保存了四次 此类型的图像: 现在,若我选择两个图像并单击发送按钮,我将在数据库中找到图像的4行名称。第一个和第二个是唯一字段,另外两个是第二个字段的重复名称 void main() { runApp( MyApp(), ); } class MyApp extends Stat

我尝试一次保存一组图像。这些图像已成功保存,但每个图像都有一个名称+路径。这些数据保存在MySQL数据库中,但问题是保存过程会重复多次。这意味着如果我保存两张图片,我会在数据库中发现这些图片已经保存了四次

此类型的图像:

现在,若我选择两个图像并单击发送按钮,我将在数据库中找到图像的4行名称。第一个和第二个是唯一字段,另外两个是第二个字段的重复名称




void main() {
  runApp(
    MyApp(),

  );
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: PickImages(),
    );
  }
}
class PickImages extends StatefulWidget {
  @override
  _PickImagesState createState() => _PickImagesState();
}

class _PickImagesState extends State<PickImages> {
  static final String uploadEndPoint = 'https://******************.php';
  List<Object> images = List<Object>();
  Future<File> _imageFile;
  bool _isVisible = false;
  String base64Image;
  List imagecode=[];
  List name=[];
  Future _onAddImageClick(int index, int type) async {
    if (images != null)
      setState(() {
        // ignore: deprecated_member_use
        _imageFile = ImagePicker.pickImage(
          source: type == 1 ? ImageSource.camera : ImageSource.gallery,
          imageQuality: 50,
        );
        getFileImage(index);
      });
  }

  void getFileImage(int index) async {
    _imageFile.then((file) async {
      setState(() {
        ImageUploadModel imageUpload = new ImageUploadModel();
        imageUpload.imageFile = file;
        images.replaceRange(index, index + 1, [imageUpload]);
      });
    });
  }

  void showImageBox() {
    setState(() {
      _isVisible = !_isVisible;
    });
  }

  @override
  void initState() {
    super.initState();
    setState(() {
      images.add("Add Image");
      images.add("Add Image");
      images.add("Add Image");
      images.add("Add Image");
      images.add("Add Image");
      images.add("Add Image");
    });
  }


  startUpload() {
    for(int i=0;i<imagecode.length;i++){
      http.post(uploadEndPoint, body: {
        "image": imagecode[i],
        "NameImage": name[i],

      }).then((result) {
        print('showall ${ imagecode[i]}');
        if (result.statusCode == 200) {
          print('anydata ${result.statusCode}');
        } else {

        }
      });
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Post Images'),
      ),
      body: Column(
        children: <Widget>[
          Container(
            //padding: EdgeInsets.only(right: 5),
            child: Card(
              elevation: 5,
              child: ListTile(
                trailing: Icon(Icons.attachment),
                title: Text('Attachments'),
                onTap: () {
                  showImageBox();
                },
              ),
            ),
          ),
          Visibility(
            visible: _isVisible,
            child: Padding(
              padding: const EdgeInsets.only(top: 5.0, right: 5.0),
              child: GridView.count(
                shrinkWrap: true,
                crossAxisCount: 3,
                childAspectRatio: 1,
                children: List.generate(images.length, (index) {
                  if (images[index] is ImageUploadModel) {
                    ImageUploadModel uploadModel = images[index];
                    if(uploadModel.imageFile!=null){

                      //base64 image
                      List<int> imageBytes = uploadModel.imageFile.readAsBytesSync();
                      // base64Image = base64Encode(snapshot.data.readAsBytesSync());
                      base64Image = base64Encode(imageBytes); //'base64Image' holds the base64 image string
                      imagecode.add(base64Image);
                      name.add(uploadModel.imageFile.path.split("/").last);}
                    return Card(
                      clipBehavior: Clip.antiAlias,
                      child: Stack(
                        children: <Widget>[
                          Image.file(
                            uploadModel.imageFile,
                            fit: BoxFit.cover,
                            width: 300,
                            height: 300,
                          ),
                          Positioned(
                            right: 5,
                            top: 5,
                            child: InkWell(
                              child: Icon(
                                Icons.remove_circle,
                                size: 20,
                                color: Colors.red,
                              ),
                              onTap: () {
                                setState(() {
                                  images.replaceRange(
                                      index, index + 1, ['Add Image']);
                                });
                              },
                            ),
                          ),

                        ],
                      ),
                    );
                  } else {
                    return Card(
                      child: IconButton(
                        icon: Icon(Icons.camera_alt),
                        onPressed: () {
                          showModalBottomSheet(
                            context: context,
                            builder: (BuildContext context) {
                              return SafeArea(
                                child: Container(
                                  child: new Wrap(
                                    children: <Widget>[
                                      new ListTile(
                                        leading: new Icon(Icons.photo_camera),
                                        title: new Text('Camera'),
                                        onTap: () {
                                          _onAddImageClick(index, 1);
                                          Navigator.of(context).pop();
                                        },
                                      ),
                                      new ListTile(
                                          leading:
                                          new Icon(Icons.photo_library),
                                          title: new Text('Gallery'),
                                          onTap: () {
                                            _onAddImageClick(index, 2);
                                            Navigator.of(context).pop();
                                          }),
                                    ],
                                  ),
                                ),
                              );
                            },
                          );
                        },
                      ),
                    );
                  }
                }),
              ),
            ),
          ),
          RaisedButton(
            child: Text('send'),
            onPressed: () {
              startUpload();
            },
          ),
        ],
      ),
    );
  }
}

class ImageUploadModel {
  File imageFile;

  ImageUploadModel({
    this.imageFile,
  });
}



void main(){
runApp(
MyApp(),
);
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:PickImages(),
);
}
}
类PickImages扩展StatefulWidget{
@凌驾
_PickImagesState createState()=>\u PickImagesState();
}
类_PickImagesState扩展状态{
静态最终字符串uploadEndPoint='https://************************.php';
列表图像=列表();
未来图像文件;
bool _isVisible=false;
字符串基图像;
列表imagecode=[];
列表名=[];
Future\u onAddImageClick(int索引,int类型)异步{
如果(图像!=null)
设置状态(){
//忽略:不推荐的\u成员\u使用
_imageFile=ImagePicker.pickImage(
source:type==1?ImageSource.camera:ImageSource.gallery,
图像质量:50,
);
getFileImage(索引);
});
}
void getFileImage(int索引)异步{
_然后((文件)异步{
设置状态(){
ImageUploadModel imageUpload=新的ImageUploadModel();
imageUpload.imageFile=文件;
replaceRange(索引,索引+1,[imageUpload]);
});
});
}
无效showImageBox(){
设置状态(){
_isVisible=!\u isVisible;
});
}
@凌驾
void initState(){
super.initState();
设置状态(){
图像。添加(“添加图像”);
图像。添加(“添加图像”);
图像。添加(“添加图像”);
图像。添加(“添加图像”);
图像。添加(“添加图像”);
图像。添加(“添加图像”);
});
}
startUpload(){

对于(int i=0;i代码中的某个地方,您可能会将条目复制到要迭代发送的列表中,您可以通过使用一个键来避免此问题,而map键是您用作键名+路径的唯一值