skip to Main Content

I decided to pull some of my image files from firebase storage in my Flutter application because the total size of the images is about 25gb. when I want to pull the images, I get some kind of ‘firebase cannot be called’ warning, how do I solve this problem?

If you can’t get an idea about the code, here’s a hint: I’m making an application that displays images with a page-turning animation. so the firebase operation must be compatible with this.

Warning Text

I/flutter ( 6311): Error getting image URL: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
I/chatty  ( 6311): uid=10148(com.example.libsy_app) 1.ui identical 2 lines
I/flutter ( 6311): Error getting image URL: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

This my code

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:page_flip/page_flip.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(AChristmasCarol());
}

final _controller = GlobalKey<PageFlipWidgetState>();

class AChristmasCarol extends StatefulWidget {
  const AChristmasCarol({Key? key}) : super(key: key);

  @override
  State<AChristmasCarol> createState() => _AChristmasCarolState();
}

class _AChristmasCarolState extends State<AChristmasCarol> {
  final _controller = GlobalKey<PageFlipWidgetState>();

  // sharedPreferences to store lastLeftOverPage Index
  final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

  // last left over page index
  int lastLeftOverPageIndex = 0;

  // SharedPreference key to retrieve the  value of lastLeftOverPageIndex
  String lastLeftOverPageNoPrefKey = "_lastLeftOverPageNoPrefKey";

  final List<String> imagesPath = [
    '/books/A Christmas Carol/page 1.png',
    '/books/A Christmas Carol/page 2.png',
    '/books/A Christmas Carol/page 3.png',
    '/books/A Christmas Carol/page 4.png',
  ];

  late List<String> downloadUrls;

  Widget _buildDemoPage(String imageUrl) {
    return FutureBuilder(
      future: _getImageUrl(imageUrl),
      builder: (BuildContext context, AsyncSnapshot<String?> snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return Center(child: CircularProgressIndicator());
        }
        if (snapshot.hasError || snapshot.data == null) {
          return Center(child: Text('Error fetching image'));
        }
        return Scaffold(
          body: Image.network(
            snapshot.data!,
            fit: BoxFit.cover,
          ),
        );
      },
    );
  }

  Future<String?> _getImageUrl(String imageUrl) async {
    try {
      final url = await firebase_storage.FirebaseStorage.instance
          .ref()
          .child(imageUrl)
          .getDownloadURL();
      return url;
    } catch (e) {
      print('Error getting image URL: $e');
      return null;
    }
  }

  @override
  void initState() {
    // restore the previous lastLeftOverPageIndex;
    _restoreLeftOverPage();
    super.initState();
  }

  Future<void> _restoreLeftOverPage() async {
    SharedPreferences pref = await _prefs;
    lastLeftOverPageIndex =
        pref.getInt(lastLeftOverPageNoPrefKey)?.toInt() ?? 0;
    // navigate the book page index to lastLeftOverPageIndex
    _controller.currentState?.goToPage(lastLeftOverPageIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageFlipWidget(
        key: _controller,
        backgroundColor: Colors.white,
        initialIndex: lastLeftOverPageIndex,
        lastPage: Container(
            color: Colors.white, child: const Center(child: Text('The End!'))),
        children: [
          for (var imagePath in imagesPath) _buildDemoPage(imagePath),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.looks_one_outlined),
        onPressed: () {
          _controller.currentState?.goToPage(1);
        },
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void deactivate() {
    super.deactivate();
    // before disposing the widget save the current page no
    var currentPageNo = _controller.currentState?.pageNumber.toInt() ?? 0;
    saveLastLeftOverPagePref(currentPageNo);
  }

  Future<void> saveLastLeftOverPagePref(int lastPage) async {
    SharedPreferences pref = await _prefs;
    pref.setInt(lastLeftOverPageNoPrefKey, lastPage);
  }
}

2

Answers


  1. Widget _buildDemoPage(String imageUrl) {
      return FutureBuilder(
        future: Firebase.initializeApp().then((_) => _getImageUrl(imageUrl)),
        builder: (BuildContext context, AsyncSnapshot<String?> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(child: CircularProgressIndicator());
          }
          if (snapshot.hasError || snapshot.data == null) {
            return Center(child: Text('Error fetching image'));
          }
          return Scaffold(
            body: Image.network(
              snapshot.data!,
              fit: BoxFit.cover,
            ),
          );
        },
      );
    }
    

    i think u should try this : modify your _buildDemoPage method

    Login or Signup to reply.
  2. It seems like you are not passing the FirebaseOptions (basically the config object) to the initializer method. Please follow the steps to configure your Firebase project and to solve this issue.

    First, run the following command to enable FlutterFire CLI tool and setup your Firebase project to work with your Flutter App

    dart pub global activate flutterfire_cli
    
    flutterfire configure
    

    Note that you’ll need Firebase CLI for the same. If you haven’t installed Firebase CLI, you need to install it first. Follow the documentation here.

    Once you complete flutterfire configure command execution, import the generated firebase_options.dart file into your main.dart.

    import 'firebase_options.dart';
    

    After that, replace the following line in your main method:

      await Firebase.initializeApp();
    

    With this:

      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
    

    You’re good to go! Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search