dart flutter上传图片到firebase的问题

yzuktlbb  于 2024-01-04  发布在  Flutter
关注(0)|答案(2)|浏览(116)

我正在尝试上传一个图片用户的所有数据都存储成功,图片也上传了,但图片有问题,Firebase storage中的图片没有**类型 *
从物理设备中删除映像
Uploading an image from the physical device
认证
Authentication
实时数据库
Realtime database
当我使用模拟器一切都很好,但当我使用物理设备,我看到这个错误
从模拟器中移除图像
Uploading an image from the emulator
应用检查
App Check

**我该如何解决这个问题?

主.dart文件

import 'package:firebase_app_check/firebase_app_check.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

import 'authentication/login_screen.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  await Permission.locationWhenInUse.isDenied.then((valueOfPermission)
  {
    if(valueOfPermission)
    {
      Permission.locationWhenInUse.request();
    }
  });
  await FirebaseAppCheck.instance.activate(

    // You can also use a `ReCaptchaEnterpriseProvider` provider instance as an
    // argument for `webProvider`
    // webProvider: ReCaptchaV3Provider('recaptcha-v3-site-key'),
    // Default provider for Android is the Play Integrity provider. You can use the "AndroidProvider" enum to choose
    // your preferred provider. Choose from:
    // 1. Debug provider
    // 2. Safety Net provider
    // 3. Play Integrity provider
    androidProvider: AndroidProvider.debug,

    // Default provider for iOS/macOS is the Device Check provider. You can use the "AppleProvider" enum to choose
    // your preferred provider. Choose from:
    // 1. Debug provider
    // 2. Device Check provider
    // 3. App Attest provider
    // 4. App Attest provider with fallback to Device Check provider (App Attest provider is only available on iOS 14.0+, macOS 14.0+)

    appleProvider: AppleProvider.appAttest,

  );
    runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Drivers App',
      debugShowCheckedModeBanner: false,
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: Colors.black,
      ),
      home: const LoginScreen(),
    );
  }
}

字符串
sign_Up.dart文件

import 'dart:io';

import 'package:drivers_app/pages/dashboard.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

import '../methods/common_methods.dart';
import '../widgets/loading_dialog.dart';
import 'login_screen.dart';

class SignUpScreen extends StatefulWidget {
  const SignUpScreen({super.key});

  @override
  State<SignUpScreen> createState() => _SignUpScreenState();
}

class _SignUpScreenState extends State<SignUpScreen> {
  TextEditingController userNameTextEditingController= TextEditingController();
  TextEditingController userPhoneTextEditingController= TextEditingController();
  TextEditingController emailTextEditingController= TextEditingController();
  TextEditingController passwordTextEditingController= TextEditingController();
  TextEditingController vehicleModelTextEditingController= TextEditingController();
  TextEditingController vehicleColorTextEditingController= TextEditingController();
  TextEditingController vehicleNumberTextEditingController= TextEditingController();


  CommonMethods cMethods = CommonMethods();
  XFile? imageFileDriver;
  String urlOfUploadedImageDriver = "";

  XFile? imageFileARCar;
  String urlOfUploadedImageARCar = "";

  XFile? imageFileRegistrationPlateCar;
  String urlOfUploadedImageRegistrationPlateCar = "";


  checkIfNetworkIsAvailable(){
    cMethods.checkConnectivity(context);
    if(imageFileDriver != null && imageFileARCar!= null && imageFileRegistrationPlateCar !=null) {//image validation
      signUpFormValidation();
    }
    else if(imageFileDriver == null)
    {
      cMethods.displaySnackBar("Please choose your image first.", context);
    }
    else if(imageFileARCar== null)
    {
        cMethods.displaySnackBar("Please choose your car image first.", context);
    }
    else if(imageFileRegistrationPlateCar ==null){
      cMethods.displaySnackBar("Please choose your vehicle registration plate image first.", context);
    }
    else{
      cMethods.displaySnackBar("Please choose your image, your car image, your vehicle registration plate image first.", context);
    }
  }

  signUpFormValidation()
  {
    if(userNameTextEditingController.text.trim().length < 3)
    {
      cMethods.displaySnackBar("your name must be at least 4 or more characters.", context);
    }
    else if(userPhoneTextEditingController.text.trim().length < 8)
    {
      cMethods.displaySnackBar("your phone number must be at least 8 or more characters.", context);
    }
    else if(!emailTextEditingController.text.contains("@"))
    {
      cMethods.displaySnackBar("please write valid email.", context);
    }
    else if(passwordTextEditingController.text.trim().length < 6)
    {
      cMethods.displaySnackBar("your password must be at least 6 or more characters.", context);
    }
    else if(vehicleModelTextEditingController.text.trim().isEmpty)
    {
      cMethods.displaySnackBar("please enter your car model.", context);
    }
    else if(vehicleColorTextEditingController.text.trim().isEmpty)
    {
      cMethods.displaySnackBar("please enter your car color.", context);
    }
    else if(vehicleNumberTextEditingController.text.isEmpty)
    {
      cMethods.displaySnackBar("please enter your car number.", context);
    }
    else
      {
        uploadImageToStorage1();
      }
  }
  uploadImageToStorage1() async
  {
    String imageIDNameDriver = DateTime.now().millisecondsSinceEpoch.toString();
    Reference referenceImageDriver = FirebaseStorage.instance.ref().child("Images").child(imageIDNameDriver);
    UploadTask uploadTaskDriver = referenceImageDriver.putFile(File(imageFileDriver!.path));

    TaskSnapshot snapshotDriver = await uploadTaskDriver;
    urlOfUploadedImageDriver = await snapshotDriver.ref.getDownloadURL();
    setState(() {
      urlOfUploadedImageDriver;
    });
    uploadImageToStorage2();

  }

  uploadImageToStorage2() async
  {

    String imageIDNameARCar = DateTime.now().millisecondsSinceEpoch.toString();
    Reference referenceImageARCar = FirebaseStorage.instance.ref().child("Images").child(imageIDNameARCar);
    UploadTask uploadTaskARCar = referenceImageARCar.putFile(File(imageFileARCar!.path));
    print("wewe "+imageFileARCar!.path);
    TaskSnapshot snapshotARCar = await uploadTaskARCar;
    urlOfUploadedImageARCar = await snapshotARCar.ref.getDownloadURL();
    setState(() {
      urlOfUploadedImageARCar;
    });
    uploadImageToStorage3();
  }
  uploadImageToStorage3() async
  {

    String imageIDNameRegistrationPlateCar = DateTime.now().millisecondsSinceEpoch.toString();
    Reference referenceImageRegistrationPlateCar = FirebaseStorage.instance.ref().child("Images").child(imageIDNameRegistrationPlateCar);
    UploadTask uploadTaskRegistrationPlateCar = referenceImageRegistrationPlateCar.putFile(File(imageFileRegistrationPlateCar!.path));
    TaskSnapshot snapshotRegistrationPlateCar = await uploadTaskRegistrationPlateCar;
    urlOfUploadedImageRegistrationPlateCar = await snapshotRegistrationPlateCar.ref.getDownloadURL();
    setState(() {
      urlOfUploadedImageRegistrationPlateCar;
    });

    registerNewDriver();
  }

  registerNewDriver() async
  {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) => LoadingDialog(messageText: "Registering your account..."),
    );

    final User? userFirebase = (
      await FirebaseAuth.instance.createUserWithEmailAndPassword(
        email: emailTextEditingController.text.trim(),
        password: passwordTextEditingController.text.trim(),
      ).catchError((errorMsg)
      {
        Navigator.pop(context);
        cMethods.displaySnackBar(errorMsg.toString(), context);
      })
    ).user;

    if(!context.mounted) return;
    Navigator.pop(context);

    DatabaseReference usersRef = FirebaseDatabase.instance.ref().child("drivers").child(userFirebase!.uid);
    Map driverCarInfo =
    {
      "carColor": vehicleColorTextEditingController.text.trim(),
      "carModel": vehicleModelTextEditingController.text.trim(),
      "carNumber": vehicleNumberTextEditingController.text.trim(),
    };
    Map driverDataMap =
    {
      "photoDriver": urlOfUploadedImageDriver,
      "photoARCar": urlOfUploadedImageARCar,
      "photoRegistrationPlateCar": urlOfUploadedImageRegistrationPlateCar,
      "car_details": driverCarInfo,
      "name": userNameTextEditingController.text.trim(),
      "email": emailTextEditingController.text.trim(),
      "phone": userPhoneTextEditingController.text.trim(),
      "id": userFirebase.uid,
      "blockStatus": "no",
    };
    usersRef.set(driverDataMap);
    Navigator.push(context, MaterialPageRoute(builder: (c)=> Dashboard()));

  }

  chooseImageFromGalleryDriver() async
  {
    final pickedFileDriver = await ImagePicker().pickImage(source: ImageSource.gallery);

    if(pickedFileDriver != null)
    {
      setState(() {
        imageFileDriver = pickedFileDriver;
      });
    }
  }

  chooseImageFromGalleryARCar() async
  {
    final pickedFileARCar = await ImagePicker().pickImage(source: ImageSource.gallery);

    if(pickedFileARCar != null)
    {
      setState(() {
        imageFileARCar = pickedFileARCar;
      });
    }
  }

  chooseImageFromGalleryRegistrationPlateCar() async
  {
    final pickedFileRegistrationPlateCar = await ImagePicker().pickImage(source: ImageSource.gallery);

    if(pickedFileRegistrationPlateCar != null)
    {
      setState(() {
        imageFileRegistrationPlateCar = pickedFileRegistrationPlateCar;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(10),
          child: Column(
            children: [

              const SizedBox(
                height: 40,
              ),

              imageFileDriver == null ?
              const CircleAvatar(
                radius: 86,
                backgroundImage: AssetImage("assets/images/avatarman.png"),
              ): Container(
                width: 180,
                height: 180,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.grey,
                  image: DecorationImage(
                    fit: BoxFit.fitHeight,
                    image: FileImage(
                      File(
                        imageFileDriver!.path,

                      ),
                    )
                  )
                ),
              ),

              const SizedBox(
                height: 10,
              ),

              GestureDetector(
                onTap: ()
                {
                  chooseImageFromGalleryDriver();

                },
                child: const Text(
                  "Choose your Image",
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),

              const SizedBox(
                height: 40,
              ),

              imageFileARCar == null ?
              const Image(
                image: AssetImage("assets/images/uberexec.png"),
              ): Container(
                width: 180,
                height: 180,
                decoration: BoxDecoration(
                    color: Colors.grey,
                    image: DecorationImage(
                        fit: BoxFit.fitHeight,
                        image: FileImage(
                          File(
                            imageFileARCar!.path,

                          ),
                        )
                    )
                ),
              ),

              const SizedBox(
                height: 10,
              ),

              GestureDetector(
                onTap: ()
                {
                  chooseImageFromGalleryARCar();

                },
                child: const Text(
                  "Choose your car Image",
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),

              const SizedBox(
                height: 40,
              ),

              imageFileRegistrationPlateCar == null ?
              const Image(
                image: AssetImage("assets/images/logo.png"),
              ): Container(
                width: 180,
                height: 180,
                decoration: BoxDecoration(
                    color: Colors.grey,
                    image: DecorationImage(
                        fit: BoxFit.fitHeight,
                        image: FileImage(
                          File(
                            imageFileRegistrationPlateCar!.path,

                          ),
                        )
                    )
                ),
              ),

              const SizedBox(
                height: 10,
              ),

              GestureDetector(
                onTap: ()
                {
                  chooseImageFromGalleryRegistrationPlateCar();

                },
                child: const Text(
                  "Choose your vehicle registration plate Image",
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),

              //text fields + button
              Padding(
                padding: const EdgeInsets.all(22),
                child: Column(
                  children: [

                    TextField(
                      controller: userNameTextEditingController,
                      keyboardType: TextInputType.text,
                      decoration: const InputDecoration(
                        labelText: "your Name",
                        labelStyle: TextStyle(
                          fontSize: 14,
                        ),

                      ),
                      style: const TextStyle(
                        color: Colors.grey,
                        fontSize: 15,
                      ),

                    ),
                    const SizedBox(height: 22,),

                    TextField(
                      controller: userPhoneTextEditingController,
                      keyboardType: TextInputType.text,
                      decoration: const InputDecoration(
                        labelText: "your Phone",
                        labelStyle: TextStyle(
                          fontSize: 14,
                        ),

                      ),
                      style: const TextStyle(
                        color: Colors.grey,
                        fontSize: 15,
                      ),

                    ),
                    const SizedBox(height: 22,),

                    TextField(
                      controller: emailTextEditingController,
                      keyboardType: TextInputType.emailAddress,
                      decoration: const InputDecoration(
                        labelText: "your Email",
                        labelStyle: TextStyle(
                          fontSize: 14,
                        ),

                      ),
                      style: const TextStyle(
                        color: Colors.grey,
                        fontSize: 15,
                      ),

                    ),
                    const SizedBox(height: 22,),

                    TextField(
                      controller: passwordTextEditingController,
                      obscureText: true,
                      keyboardType: TextInputType.text,
                      decoration: const InputDecoration(
                        labelText: "your Password",
                        labelStyle: TextStyle(
                          fontSize: 14,
                        ),

                      ),
                      style: const TextStyle(
                        color: Colors.grey,
                        fontSize: 15,
                      ),

                    ),
                    const SizedBox(height: 32,),

                    TextField(
                      controller: vehicleModelTextEditingController,
                      keyboardType: TextInputType.text,
                      decoration: const InputDecoration(
                        labelText: "your car Model",
                        labelStyle: TextStyle(
                          fontSize: 14,
                        ),

                      ),
                      style: const TextStyle(
                        color: Colors.grey,
                        fontSize: 15,
                      ),

                    ),
                    const SizedBox(height: 22,),
                    TextField(
                      controller: vehicleColorTextEditingController,
                      keyboardType: TextInputType.text,
                      decoration: const InputDecoration(
                        labelText: "your Car Color",
                        labelStyle: TextStyle(
                          fontSize: 14,
                        ),

                      ),
                      style: const TextStyle(
                        color: Colors.grey,
                        fontSize: 15,
                      ),

                    ),
                    const SizedBox(height: 22,),
                    TextField(
                      controller: vehicleNumberTextEditingController,
                      keyboardType: TextInputType.text,
                      decoration: const InputDecoration(
                        labelText: "your Car Number",
                        labelStyle: TextStyle(
                          fontSize: 14,
                        ),

                      ),
                      style: const TextStyle(
                        color: Colors.grey,
                        fontSize: 15,
                      ),

                    ),
                    const SizedBox(height: 22,),

                    ElevatedButton(
                      onPressed: ()
                      {
                        checkIfNetworkIsAvailable();

                      },
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.purple,
                        padding: const EdgeInsets.symmetric(horizontal: 80,vertical: 10)
                      ),
                      child: const Text(
                        "Sign Up"
                      ),
                    ),

                  ],
                ),
              ),

              const SizedBox(height: 12,),

              //textbutton
              TextButton(
                onPressed: ()
                {
                  Navigator.push(context, MaterialPageRoute(builder: (c)=> LoginScreen()));

                },
                child: const Text(
                  "Already have an Account? Login Here",
                  style: TextStyle(
                    color: Colors.grey,
                  ),
                ),
              ),

            ],
          ),
        ),
      ),
    );
  }
}


yaml文件

name: drivers_app
description: This Boombest_Driver Drivers App
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.0+1

environment:
  sdk: '>=3.1.3 <4.0.0'

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  connectivity_plus:
  firebase_core:
  firebase_auth:
  firebase_database:
  firebase_storage:
  google_maps_flutter:
  permission_handler:
  geolocator:
  image_picker:
  firebase_app_check: ^0.2.1+6
  cloud_firestore: ^4.13.3

dev_dependencies:
  flutter_test:
    sdk: flutter

  http:

  # The "flutter_lints" package below contains a set of recommended lints to
  # encourage good coding practices. The lint set provided by the package is
  # activated in the `analysis_options.yaml` file located at the root of your
  # package. See that file for information about deactivating specific lint
  # rules and activating additional ones.
  flutter_lints: ^2.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
linter:
  rules:
    - use_super_parameters
# The following section is specific to Flutter packages.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/images/
    - themes/

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

pod7payv

pod7payv1#

尝试手动指定类型;
在uploadImageToString 1()中更改

Reference referenceImageDriver = FirebaseStorage.instance.ref().child("Images").child(imageIDNameDriver);

字符串

Reference referenceImageDriver = FirebaseStorage.instance.ref().child("Images").child('$imageIDNameDriver.jpg');


uploadImageToString 2()也是如此。

eagi6jfj

eagi6jfj2#

您需要在ref.putFile中指定图像的类型。

ref.putFile(mediaFilePath, SettableMetadata(contentType: 'image/png'));

字符串

相关问题