Search code examples
flutterfirebaseflutter-dependencies

Flutter: After Changes won't run on mobile platforms


Description: I encountered an issue while fixing data loss problems in my web build that occurred upon refreshing. In my attempts to address this issue, I made changes including adding image_picker_web to the project. This change seemed to trigger a new problem. However, I have since removed image_picker_web, and now I'm facing errors on both the iOS and Android platforms. It's worth noting that the web version is working perfectly without any issues.

error log:


    Launching lib\main.dart on Infinix X6831 in debug mode...
    main.dart:1
    : Error: The argument type 'String?' can't be assigned to the parameter type 'Object' because 'String?' is nullable and 'Object' isn't.
    element_subclasses.dart:2752
    
    'Object' is from 'dart:core'.
    final parsed = css.parse(text);
    ^
    Target kernel_snapshot failed: Exception
    2
    
    FAILURE: Build failed with an exception.
    
    Where:
    Script 'C:\scr\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1157
    
    What went wrong:
    Execution failed for task ':app:compileFlutterBuildDebug'.
    
    Process 'command 'C:\scr\flutter\bin\flutter.bat'' finished with non-zero exit value 1
    
    Try:
    Run with --stacktrace option to get the stack trace.
    Run with --info or --debug option to get more log output.
    Run with --scan to get full insights.
    
    Get more help at https://help.gradle.org
    BUILD FAILED in 13s
    Exception: Gradle task assembleDebug failed with exit code 1
    Exited (sigterm)

Code Snippet (main.dart):


    import 'package:auto_route/auto_route.dart';
    import 'package:easy_localization/easy_localization.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:flutter_bloc/flutter_bloc.dart';
    import 'package:flutter_screenutil/flutter_screenutil.dart';
    import 'package:kleely/services/exam_service.dart';
    import 'package:kleely/services/question_service.dart';
    import 'package:overlay_support/overlay_support.dart';
    import 'package:provider/provider.dart';
    
    import 'firebase_options.dart';
    import 'router/garde.dart';
    import 'router/router.gr.dart';
    import 'services/chapter_service.dart';
    import 'services/class_service.dart';
    import 'services/grade.service.dart';
    import 'services/sub_chapter_service.dart';
    import 'services/subject_service.dart';
    import 'services/user_authentication.dart';
    import 'translations/codegen_loader.g.dart';
    import 'utils/injection/injection_container.dart';
    import 'utils/network/internet_checker.dart';
    import 'utils/network/no_internet.dart';
    import 'utils/theme/app_theme.dart';
    import 'utils/theme/presentation/theme_cubit.dart';
    
    
    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await EasyLocalization.ensureInitialized();
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
      ]);
    
      await init();
      if (kIsWeb) {
        await Firebase.initializeApp(
          options: DefaultFirebaseOptions.currentPlatform,
        );
    
        runApp(
          OverlaySupport.global(
            child: EasyLocalization(
              path: 'assets/translations',
              supportedLocales: const [Locale('ar')],
              useFallbackTranslations: true,
              fallbackLocale: const Locale('ar'),
              assetLoader: const CodegenLoader(),
              startLocale: const Locale('ar'),
              child: MainApp(),
            ),
          ),
        );
      } else {
        bool isConnected = await sl<InternetChecker>().checkInternet();
        if (isConnected) {
          await Firebase.initializeApp(
            options: DefaultFirebaseOptions.currentPlatform,
          );
    
          runApp(
            OverlaySupport.global(
              child: EasyLocalization(
                path: 'assets/translations',
                supportedLocales: const [Locale('ar')],
                useFallbackTranslations: true,
                fallbackLocale: const Locale('ar'),
                assetLoader: const CodegenLoader(),
                startLocale: const Locale('ar'),
                child: MainApp(),
              ),
            ),
          );
        } else {
          runApp(const NoInternet());
        }
      }
    }
    
    class MainApp extends StatelessWidget {
      MainApp({super.key});
    
      final _appRouter = AppRouter(authGuard: AuthGuard());
    
      @override
      Widget build(BuildContext context) {
        return MultiProvider(
          providers: [
            ChangeNotifierProvider(create: (_) => UserAuthentication()),
            Provider(create: (_) => GradeService()),
            Provider(create: (_) => SubjectService()),
            Provider(create: (_) => ChapterService()),
            Provider(create: (_) => SubChapterService()),
            Provider(create: (_) => ClassService()),
            Provider(create: (_) => ExamService()),
            Provider(create: (_) => QuestionService()),
            BlocProvider<ThemeCubit>(create: (_) {
              // Initialize ThemeCubit and load the saved theme mode
              final themeCubit = sl<ThemeCubit>();
              themeCubit.loadThemeMode();
              return themeCubit;
            })
          ],
          child: BlocBuilder<ThemeCubit, ThemeMode>(
            builder: (context, themeMode) {
              return MaterialApp.router(
                // easy localization
                localizationsDelegates: context.localizationDelegates,
                supportedLocales: context.supportedLocales,
                locale: context.locale,
    
                title: 'Kleely',
                debugShowCheckedModeBanner: false,
    
                // app theme
                theme: themeMode == ThemeMode.light
                    ? AppTheme.themeData
                    : AppTheme.darkTheme,
                darkTheme: AppTheme.darkTheme,
                themeMode: themeMode,
    
                routerDelegate: AutoRouterDelegate(
                  _appRouter,
                  navigatorObservers: () => [AutoRouteObserver()],
                ),
                routeInformationParser: _appRouter.defaultRouteParser(),
                builder: (context, router) {
                  ScreenUtil.init(
                    context,
                    designSize: const Size(360, 690),
                  );
    
                  return router!;
                },
              );
            },
          ),
        );
      }
    }

Environment:


    Flutter version: 3.7.12
    Dart version: 2.19.6
    Operating system: Windows 10
    Android Studio version: 2021.3
    Visual Studio Code version: 1.81.1

Dependencies:

yaml


    name: kleely
    description: A new Flutter project.
    publish_to: "none"
    version: 0.1.0
    
    environment:
      sdk: ">=2.19.6 <3.0.0"
    
    dependencies:
      flutter:
        sdk: flutter
    
      # firebase
      firebase_core: ^2.15.0
      firebase_auth: ^4.7.1
      cloud_firestore: ^4.8.3
      firebase_storage: ^11.2.5
    
      # other
      http: ^0.13.6
      intl: ^0.17.0
      permission_handler: ^10.4.3
      dartz:
    
      # route
      path_provider: ^2.1.0
      auto_route: ^5.0.2
    
      # Generators
      username_generator: ^1.3.0
      uuid: ^3.0.7
    
      # style
      flex_color_scheme: ^7.0.5
      flutter_carousel_widget: ^2.0.4
      flutter_screenutil: ^5.7.0
      flutter_spinkit: ^5.2.0
      google_fonts: ^4.0.4
      shimmer: ^3.0.0
      curved_navigation_bar: ^1.0.3
      percent_indicator: ^4.2.3
      infinite_carousel: ^1.0.2
      intl_phone_number_input: ^0.7.3+1
      loading_animation_widget: ^1.2.0+4
      widget_and_text_animator: ^1.0.0
    
      # localization
      easy_localization: ^3.0.2
    
      # network and overlay
      connectivity_plus: ^4.0.2
      overlay_support: ^2.1.0
    
      # video and image
      appinio_video_player: ^1.2.1
      image_picker: ^1.0.1
      # image_picker_web: ^3.0.0+1
      image_cropper: ^1.4.1
    
      # storage
      hive: ^2.2.3
      hive_flutter: ^1.1.0
    
      # state management & dependency injection
      get: ^4.6.5
      get_it: ^7.6.0
      bloc: ^8.1.2
      flutter_bloc: ^8.1.3
      provider: ^6.0.5
      image_picker_for_web: ^3.0.0
      cached_network_image: ^3.2.3
      blurry_modal_progress_hud: ^1.1.1
      firebase_performance: ^0.9.2+5
      universal_html: ^2.2.2
    
    dev_dependencies:
      flutter_test:
        sdk: flutter
    
      flutter_lints: ^2.0.0
      auto_route_generator: ^5.0.2
      build_runner: ^2.3.3
      flutter_launcher_icons: ^0.13.1
    
    flutter_icons:
      android: true
      ios: true
      image_path: "assets/logos/icon.png"
    
    # For information on the generic Dart part of this file, see the
    flutter:
      uses-material-design: true
    
      assets:
        - assets/logos/KleeliLogo.png
        - assets/logos/KleeliIcon.ico
        - assets/icons/1.png
        - assets/icons/2.png
        - assets/icons/3.png
        - assets/icons/4.png
        - assets/icons/biology.png
        - assets/icons/chemistry.png
        - assets/icons/physics.png
        - assets/icons/math.png
        - assets/translations/
        - assets/images/c.jpg
        - assets/audio/loading.mp3
    ```

Recent Changes:
Future<Either<Failure, Student>> fetchStudent(String studentId) async {
    DocumentSnapshot docSnapshot =
        await _firestore.collection('students').doc(studentId).get();
    if (docSnapshot.exists) {
      final studentData = docSnapshot.data() as Map<String, dynamic>;
      final student = Student.fromMap(studentData);
      notifyListeners();

      if (kIsWeb) {
        // Store student data in cookies on web
        CookieManager.setObjectAsCookie('student', student.toMap());
      } else {
        // Store student data in Hive on mobile
        await HiveManager.addObjectAsBox('student', student.toMap());
      }

      return right(student);
    } else {
      _student = null;
      notifyListeners();
      return left(Failure('No student found with this ID'));
    }
  }

GestureDetector(
                onTap: () async {
                  if (kIsWeb) {
                    final ImagePicker _picker = ImagePicker();
                    XFile? image =
                        await _picker.pickImage(source: ImageSource.gallery);
                    if (image != null) {
                      File file = File(image.path);
                      // ignore: use_build_context_synchronously
                      await Provider.of<UserAuthentication>(context,
                              listen: false)
                          .setProfilePicture(file);
                    }
                  } else {
                    PermissionStatus permissionStatus =
                        await Permission.photos.status;

                    if (permissionStatus.isDenied ||
                        permissionStatus.isPermanentlyDenied) {
                      // You can show a Dialog here to inform the user that the app needs the permission
                      permissionStatus = await Permission.photos.request();
                    }

                    if (permissionStatus.isGranted) {
                      final XFile? image = await ImagePicker()
                          .pickImage(source: ImageSource.gallery);

                      if (image != null) {
                        File? croppedFile = await ImageCropper().cropImage(
                          sourcePath: image.path,
                          compressFormat: ImageCompressFormat.jpg,
                          aspectRatioPresets: [
                            CropAspectRatioPreset.square,
                            CropAspectRatioPreset.ratio3x2,
                            CropAspectRatioPreset.original,
                            CropAspectRatioPreset.ratio4x3,
                            CropAspectRatioPreset.ratio16x9
                          ],
                          androidUiSettings: AndroidUiSettings(
                              toolbarTitle: 'Cropper',
                              toolbarColor: Colors.white,
                              // ignore: use_build_context_synchronously
                              toolbarWidgetColor:
                                  Theme.of(context).primaryColor,
                              initAspectRatio: CropAspectRatioPreset.original,
                              lockAspectRatio: false),
                          iosUiSettings: const IOSUiSettings(
                            title: 'Cropper',
                          ),
                        );

                        if (croppedFile != null) {
                          // ignore: use_build_context_synchronously
                          await Provider.of<UserAuthentication>(context,
                                  listen: false)
                              .setProfilePicture(croppedFile);
                        }
                      }
                    } else {
                      // Handle the scenario when the user declines the permission
                      log("Permission to access gallery is denied");
                    }
                  }
                },
                child: Consumer<UserAuthentication>(
                  builder: (context, value, child) {
                    return SizedBox(
                      width: 100.w, // using screenutil extension
                      child: CircleAvatar(
                        radius: 50.r, // using screenutil extension
                        backgroundImage: NetworkImage(
                          value.user == null
                              ? 'https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?r=pg'
                              : value.user!.photoURL ??
                                  'https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?r=pg', // Replace with your image URL
                        ),
                      ),
                    );
                  },
                ),
              ),
```

Solution

  • A few suggestions:

    • Delete pubspec.lock
    • Run flutter clean
    • Run flutter pub get
    • Upgrade Flutter if your project allows it
    • Use the newest version of the image_picker package which looks like it adds compatibility with the web package.

    enter image description here