颤振:Firebase存储没有上载多个图像,它总是重复相同的路径吗? class CategoryScreen扩展StatefulWidget{ 静态常量routeName='/category screen'; @凌驾 _CategoryScreenState createState()=>U CategoryScreenState(); } 类_CategoryScreenState扩展状态{ @凌驾 小部件构建(构建上下文){ //来自co_detai_screen.dart final companyId=ModalRoute.of(context).settings.arguments作为字符串; // 最终形式=GlobalKey(); 字符串_car=''; 字符串_carModel=''; 档案carImage; String ranDom=randomBefore(0,300).toString(); void _submitCarCategory()异步{ final isValid=_form.currentState.validate(); 如果(有效){ _form.currentState.save(); //…在这里上传图片 /*===================================================这是firebase存储工作==============================*/ final ref=FirebaseStorage.instance.ref().child('cars').child(ranDom+'.jpg'); 等待参考文件(carImage); //kan fe.onComplete ba3ed(图像)**强文本** //…在这里上传图片 //…现在想把这张照片放到firestore里吗 最终url=等待参考getDownloadURL(); //…现在想把这张照片放到firestore里吗 FirebaseFirestore.instance.collection('companys').doc(companyId).collection('category').doc().set(({ “卡纳姆”:“你的车, “卡莫代尔”:卡莫代尔, “汽车图像”:url })); Navigator.of(context.pop(); } } void\u pickedImage(文件映像){ carImage=图像; } void_showDialog(){ showDialog(上下文:上下文,生成器:(上下文)=>AlertDialog( 标题:文本(“添加类别”), 内容:SingleChildScrollView( 孩子:表格( 键:_形式, 子:容器( 身高:450, 宽度:double.infinity, 填充:边缘设置。全部(20), 子:列( 儿童:[ 文本('添加类别',样式:TextStyle(fontSize:16),), 尺寸箱(高度:10,), UserImagePicker(_pickedImage), TextFormField(装饰:输入装饰(labelText:'Car'), 已保存:(值){ _汽车=价值; }, ), TextFormField( 装饰:输入装饰(labelText:“汽车模型”), 已保存:(值){ _carModel=数值; }, ), 尺寸箱(高度:20,), 扁平按钮( 已按下:(){ _submitCarCategory(); }, 子项:文本('Add'), 颜色:颜色,靛蓝, textColor:Colors.white, 形状:圆形矩形边框( 边界半径:边界半径。圆形(20) ), ), ], ), ), ), ), ), ); } 回归未来建设者( future:FirebaseFirestore.instance.collection('companys').doc(companyId.get(), 建筑商:(ctx,未来快照){ if(futuresnashot.hasData){ 返回脚手架( appBar:appBar(标题:行)( 儿童:[ 文本(futureSnapshot.data['ConName']), 尺寸标注框(宽度:5,), 文本(“类别”), ], ), ), 正文:专栏( 儿童:[ 尺寸箱(高度:30,), 划船( mainAxisAlignment:mainAxisAlignment.center, 儿童:[ 填充物( 填充:仅限常量边集(左:15.0), 孩子:扁平按钮( 按下时:()=>\u showDialog(), 子项:文本(“添加类别”), 颜色:颜色,靛蓝, textColor:Colors.white, 形状:圆形矩形边框( 边界半径:边界半径。圆形(20) ), ), ), ], ), 尺寸箱(高度:10,), 分隔符(), 尺寸箱(高度:10,), 扩大( child:CategoryWidget( //futurebuilder快照id futureshopshot.data.id ), ), ], ), ); }否则{ 返回脚手架( 主体:中心(子对象:循环压缩机指示器()), ); } } ); } }

颤振:Firebase存储没有上载多个图像,它总是重复相同的路径吗? class CategoryScreen扩展StatefulWidget{ 静态常量routeName='/category screen'; @凌驾 _CategoryScreenState createState()=>U CategoryScreenState(); } 类_CategoryScreenState扩展状态{ @凌驾 小部件构建(构建上下文){ //来自co_detai_screen.dart final companyId=ModalRoute.of(context).settings.arguments作为字符串; // 最终形式=GlobalKey(); 字符串_car=''; 字符串_carModel=''; 档案carImage; String ranDom=randomBefore(0,300).toString(); void _submitCarCategory()异步{ final isValid=_form.currentState.validate(); 如果(有效){ _form.currentState.save(); //…在这里上传图片 /*===================================================这是firebase存储工作==============================*/ final ref=FirebaseStorage.instance.ref().child('cars').child(ranDom+'.jpg'); 等待参考文件(carImage); //kan fe.onComplete ba3ed(图像)**强文本** //…在这里上传图片 //…现在想把这张照片放到firestore里吗 最终url=等待参考getDownloadURL(); //…现在想把这张照片放到firestore里吗 FirebaseFirestore.instance.collection('companys').doc(companyId).collection('category').doc().set(({ “卡纳姆”:“你的车, “卡莫代尔”:卡莫代尔, “汽车图像”:url })); Navigator.of(context.pop(); } } void\u pickedImage(文件映像){ carImage=图像; } void_showDialog(){ showDialog(上下文:上下文,生成器:(上下文)=>AlertDialog( 标题:文本(“添加类别”), 内容:SingleChildScrollView( 孩子:表格( 键:_形式, 子:容器( 身高:450, 宽度:double.infinity, 填充:边缘设置。全部(20), 子:列( 儿童:[ 文本('添加类别',样式:TextStyle(fontSize:16),), 尺寸箱(高度:10,), UserImagePicker(_pickedImage), TextFormField(装饰:输入装饰(labelText:'Car'), 已保存:(值){ _汽车=价值; }, ), TextFormField( 装饰:输入装饰(labelText:“汽车模型”), 已保存:(值){ _carModel=数值; }, ), 尺寸箱(高度:20,), 扁平按钮( 已按下:(){ _submitCarCategory(); }, 子项:文本('Add'), 颜色:颜色,靛蓝, textColor:Colors.white, 形状:圆形矩形边框( 边界半径:边界半径。圆形(20) ), ), ], ), ), ), ), ), ); } 回归未来建设者( future:FirebaseFirestore.instance.collection('companys').doc(companyId.get(), 建筑商:(ctx,未来快照){ if(futuresnashot.hasData){ 返回脚手架( appBar:appBar(标题:行)( 儿童:[ 文本(futureSnapshot.data['ConName']), 尺寸标注框(宽度:5,), 文本(“类别”), ], ), ), 正文:专栏( 儿童:[ 尺寸箱(高度:30,), 划船( mainAxisAlignment:mainAxisAlignment.center, 儿童:[ 填充物( 填充:仅限常量边集(左:15.0), 孩子:扁平按钮( 按下时:()=>\u showDialog(), 子项:文本(“添加类别”), 颜色:颜色,靛蓝, textColor:Colors.white, 形状:圆形矩形边框( 边界半径:边界半径。圆形(20) ), ), ), ], ), 尺寸箱(高度:10,), 分隔符(), 尺寸箱(高度:10,), 扩大( child:CategoryWidget( //futurebuilder快照id futureshopshot.data.id ), ), ], ), ); }否则{ 返回脚手架( 主体:中心(子对象:循环压缩机指示器()), ); } } ); } },firebase,image,flutter,firebase-storage,Firebase,Image,Flutter,Firebase Storage,而不是使用 class CategoryScreen extends StatefulWidget { static const routeName = '/category-screen'; @override _CategoryScreenState createState() => _CategoryScreenState(); } class _CategoryScreenState extends State<CategoryScreen> { @o

而不是使用

class CategoryScreen extends StatefulWidget {
  static const routeName = '/category-screen';
  @override
  _CategoryScreenState createState() => _CategoryScreenState();
}

class _CategoryScreenState extends State<CategoryScreen> {
  @override
  Widget build(BuildContext context) {
    //from co_detai_screen.dart
    final companyId = ModalRoute.of(context).settings.arguments as String;
    //
    final _form = GlobalKey<FormState>();
    String _car = '';
    String _carModel = '';
    File carImage;
    
    String ranDom = randomBetween(0, 300).toString();
    
    void _submitCarCategory() async{
    final isValid = _form.currentState.validate();
    
    if(isValid){
      _form.currentState.save();
       //.... upload image here
/*===============================Here is firebase storage work==============================*/
      final ref = FirebaseStorage.instance.ref().child('cars').child(ranDom + '.jpg');
      await ref.putFile(carImage);
      //kan fe .onComplete ba3ed (image)**strong text**
      //.... upload image here

      //... now want to get this image into the firestore
      final url = await ref.getDownloadURL();
      //... now want to get this image into the firestore
      FirebaseFirestore.instance.collection('companies').doc(companyId).collection('category').doc().set(({
        'carName': _car,
        'carModel': _carModel,
        'car_image': url
      }));
      Navigator.of(context).pop();
    }
  }


    void _pickedImage(File image){
      carImage = image;
    }

    void _showDialog(){
      showDialog(context: context, builder: (context)=> AlertDialog(
        title: Text('Add Category'),
        content: SingleChildScrollView(
          child: Form(
          key: _form,
                child: Container(
                height: 450,
                width: double.infinity,
                padding: EdgeInsets.all(20),
                  
                      child: Column(
                      children: [
                        Text('Add Category', style: TextStyle(fontSize: 16),),
                        SizedBox(height: 10,),
                        UserImagePicker(_pickedImage),
                        TextFormField(decoration: InputDecoration(labelText: 'Car'),
                         onSaved: (value){
                            _car = value;
                          },
                        ),
                        TextFormField(
                          decoration: InputDecoration(labelText: 'Car model'),
                          onSaved: (value){
                            _carModel = value;
                          },
                        ),
                        SizedBox(height: 20,),
                        FlatButton(
                          onPressed: (){
                            _submitCarCategory();
                          }, 
                          child: Text('Add'),
                          color: Colors.indigo,
                          textColor: Colors.white,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(20)
                          ),
                          ),
                      ],
                  ),
                    ),
                    ),
    ),
      ),
      );
    }


    return FutureBuilder(
      future: FirebaseFirestore.instance.collection('companies').doc(companyId).get(),
      builder: (ctx, futureSnapshot){
        if(futureSnapshot.hasData){
          return Scaffold(
            appBar: AppBar(title: Row(
              children: [
                Text(futureSnapshot.data['coname']),
                SizedBox(width: 5,),
                Text('Category'),
              ],
            ),
            ),
            body: Column(
                children: [
                  SizedBox(height: 30,),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Padding(
                        padding: const EdgeInsets.only(left: 15.0),
                        child: FlatButton(
                          onPressed: ()=> _showDialog(), 
                          child: Text('Add Category'),
                          color: Colors.indigo,
                          textColor: Colors.white,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(20)
                          ),
                        ),
                      ),
                  ],
                  ),

                  SizedBox(height: 10,),
                  Divider(),
                  SizedBox(height: 10,),
                 
                  Expanded(
                    child: CategoryWidget(
                      //futurebuilder snapshot id
                      futureSnapshot.data.id
                      ),
                  ),
                
                ],
              ),
          );
        }else{
          return Scaffold(
            body: Center(child: CircularProgressIndicator()),
          );
        }
      }
      );
  }
}
为图像名称指定日期和时间(包括秒),或同时指定ranDom和dateTime

 String ranDom = randomBetween(0, 300).toString();

请注意:它工作正常,但在firebase存储中,当它上载图像时,例如:154.jpg,一切正常,但当我上载另一个图像时,它会重复154.jpg,它不会给出另一个随机数,如200.jpg,这使应用程序崩溃。您很好,谢谢您,它工作得很好。欢迎
final ref = FirebaseStorage.instance.ref().child('cars').child(ranDom + DateTime + '.jpg');