No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()) - flutter

I am trying to create a SignIn page with Firebase authentication, but this error is happening in main.dart when I run the code:
This is the code: from main.dart:
import 'package:app_mypocket/services/auth_service.dart';
import 'package:app_mypocket/telas/home/homepage.dart';
import 'package:app_mypocket/telas/login/telalogin.dart';
[import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Pocket',
debugShowCheckedModeBanner: false,
theme: ThemeData(
),
home: HomePage(),
//routes: routes,
);
}
}
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) => Scaffold(
body: StreamBuilder<User?>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.hasData){
return HomePage();
} else{
return TelaLogin();
}
})
);
}
My pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
intl:
provider: ^6.0.4
shared_preferences: ^2.0.6
hive: ^2.0.4
hive_flutter: ^1.1.0
path_provider: ^2.0.2
sqflite: ^2.0.0+3
fl_chart: ^0.55.2
firebase_core: ^2.1.1
firebase_auth: ^4.1.0
cloud_firestore: ^4.0.3
Also, I have created a user manually in my project on the firebase website, so I am trying to logIn.

When logging into Firebase you normally provide options, like:
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize Firebase
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
These firebase_options are generated by running "flutterfire configure" (previously firebase configure iirc).
I think you forget this specific step. See the guide: https://firebase.google.com/docs/flutter/setup?platform=ios
The configuration file contains information about the apiKey, appId, projectId, bundle/app name and much more. Not providing it, will lead to a default/blank/unusable app.

Related

Ignoring header X-Firebase-Locale because its value was null. - Firebase

I am creating a SignIn Page, but when I try to login this error appears:
W/System (17313): Ignoring header X-Firebase-Locale because its value was null.
D/FirebaseAuth(17313): Notifying id token listeners about user ( userId ).
This is my main.dart code:
import 'package:app_mypocket/telas/login/telalogin.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
//initilization of Firebase app
// other Firebase service initialization
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Pocket',
debugShowCheckedModeBanner: false,
theme: ThemeData(
),
home: TelaLogin(),
);
}
}
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) => Scaffold(
body: StreamBuilder<User?>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.hasData){
return HomePage();
} else{
return TelaLogin();
}
})
);
}
Additional information:
The phone is connected to internet properly;
I wrote this line on manifest.xml inside application tag:
android:usesCleartextTraffic="true";
The password has 6 characters in length;
Email/Password Sign-in method on firebase console is enabled.

Can't fetch local JSON, flutter

I've followed the steps to fetch local JSON, I think the problem is because in the pubspec, when I change the name local JSON different name the console nothing error, so what issues do I have? it's my wrong or flutter update something? it's my code :
import 'dart:convert';
import 'package:fetch_local_data/model.dart';
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
List<Books> parseJson(String? json) {
if (json == null) {
return [];
}
List result = jsonDecode(json);
return result.map((e) => Books.fromJson(e)).toList();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<String>(
future: DefaultAssetBundle.of(context).loadString('assets/books.json'),
builder: (context, snapshot) {
List<Books> books = parseJson(snapshot.data);
print(books.length);
return ListView.builder(
itemCount: books.length,
itemBuilder: (context, index) {
return Text(books[index].title);
});
},
),
);
}
}
pubspec
name: fetch_local_data
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.17.6 <3.0.0"
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
flutter:
uses-material-design: true
assets:
- assets/books.json
folder structure

Flutter MultiProvider Not found

Error: Could not find the correct Provider above this AuthenticationWrapper Widget
This happens because you used a BuildContext that does not include the provider
of your choice. There are a few common scenarios:
You added a new provider in your main.dart and performed a hot-reload.
To fix, perform a hot-restart.
The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then
other routes will not be able to access that provider.
You used a BuildContext that is an ancestor of the provider you are trying to read.
Make sure that AuthenticationWrapper is under your MultiProvider/Provider.
This usually happens when you are creating a provider and trying to read it immediately
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:park_app/app_styles.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import './views/pages.dart';
import 'views/authentication/authentication_service.dart';
import 'Home_Page.dart';
bool? seenOnboard;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// to show status bar
SystemChrome.setEnabledSystemUIOverlays(
[SystemUiOverlay.bottom, SystemUiOverlay.top]);
// to load onboard for the first time only
SharedPreferences pref = await SharedPreferences.getInstance();
seenOnboard = pref.getBool('seenOnboard') ?? false; //if null set to false
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<AuthenticationService>(
create: (_) => AuthenticationService(FirebaseAuth.instance),
),
StreamProvider(
initialData: null,
create: (context) =>
context.read<AuthenticationService>().authStateChanges),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Park App',
theme: ThemeData(
textTheme: GoogleFonts.manropeTextTheme(
Theme.of(context).textTheme,
),
primarySwatch: Colors.blue,
scaffoldBackgroundColor: kScaffoldBackground,
),
home: seenOnboard == true ? AuthenticationWrapper() : OnBoardingPage(),
),
);
}
}
class AuthenticationWrapper extends StatelessWidget {
#override
Widget build(BuildContext context) {
final firebaseUser = context.watch<User>();
if (firebaseUser != null) {
return HomePage();
}
return LoginPage();
}
}
install provider by running the command below:
flutter pub add provider and then in your main.dart file, import it import 'package:provider/provider.dart';
Make sure to specify the generic type on StreamProvider:
StreamProvider<User?>(
...
)
Note that you have set null as initialData, so your widget likely needs to handle null users. Meaning you need to do:
final user = context.watch<User?>()

Flutter ':path_provider:compileDebugJavaWithJavac' Error

Im having problem in the path provider dependencies in flutter.
I've been trying to find a solution for this for almost 2 day now..
please help me..
My dependencies
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.0
hive: ^1.4.1+1
hive_flutter: ^0.3.0+2
path_provider: ^1.6.5
http: ^0.12.2
My Code (even without code its running the error as long as a add the path provider)
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:convert';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:hive/hive.dart';
import 'package:http/http.dart' as http;
void main() async {
//Hive.initFlutter();
WidgetsFlutterBinding.ensureInitialized();
//final appDocDir = await getApplicationDocumentsDirectory();
// Hive.init(appDocDir.path);
var box = await Hive.openBox('employee');
runApp(
MaterialApp(home: Home()),
);
}
class Home extends StatefulWidget {
Home({Key key}) : super(key: key);
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sample'),
),
body: Center(),
);
}
}
I've resolved this using by doing flutter pub downgrade in the terminal

A value of type 'MultiProvider' can't be returned from method 'build' because it has a return type of 'Widget'

I have tired to stream user authentication state changes using StreamProvider.value to MaterialApp. But i got this Error: A value of type 'MultiProvider' can't be returned from method 'build' because it has a return type of 'Widget'.
This are my the dependency in pubspec.yaml
firebase_core: ^0.5.0 firebase_auth: ^0.18.0+1 cloud_firestore: ^0.14.0+2 provider: ^4.3.2+2
And couldn't be able to figure it out. I'm stuck please help. Thank you for your help in advance.
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
class MyAwesomeApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [StreamProvider<Usr>.value(value: Authenticate().userStream)],
builder: (context, child) {
return MaterialApp(
home: Wrapper(),
);
},
);
}
}
//This is Authenticate class which holds the stream
import 'package:firebase_auth/firebase_auth.dart';
class Authenticate {
//create an instance of firebase auth
FirebaseAuth _auth = FirebaseAuth.instance;
// Create a new user instance from my user model
Usr _userFromFirebaseUser(User user) {
return user != null ? Usr(uid: user.uid) : null;
}
// set up auth stream to listen to user auth status
Stream<Usr> get userStream {
return _auth.authStateChanges().map(
_userFromFirebaseUser); // shorter way of mapping to user custom user object
// .map((User user) => _userFromFirebaseUser(user)); //longer one
}
// sign in anon
Future signInAnon() async {
try {
UserCredential response = await _auth.signInAnonymously();
User user = response.user;
return _userFromFirebaseUser(user);
} catch (e) {
print(e.toString());
return null;
}
}
}
Try this
import 'package:flutter/material.dart';
import 'package:provider/provider.dart' as statemanagement;
import 'models/model_provider.dart';
import 'modules/screen_root.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return statemanagement.MultiProvider(
providers: [
statemanagement.Provider<ModelProvider>(
create: ((_) => ModelProvider())),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Multi Provider',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const ScreenRoot(),
),
);
}
}