I’m having an issue with Firebase where I keep getting the error: “An internal error has occurred. [ API key not valid. Please pass a valid API key. ]”.
Here’s what I’ve tried so far:
- Verified the API key in the Firebase Console.
- Checked API key restrictions in Google Cloud Console.
- Made sure all required APIs are enabled.
- Regenerated the API key and updated the configuration files.
- Updated Firebase dependencies.
- Checked for any network-related issues.
Despite these efforts, the problem persists. Has anyone experienced this before or have any suggestions on what I might be missing?
Thanks in advance for your help!
import 'dart:ui';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:instaapp/screens/sign_up.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: FirebaseOptions(
apiKey: 'key',
appId: 'id',
messagingSenderId: 'sendid',
projectId: 'myapp',
storageBucket: 'myapp-b9yt18.appspot.com',
));
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Insta App',
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: Colors.black),
debugShowCheckedModeBanner: false,
home: const signup(),
);
}
}
import 'dart:io';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:instaapp/screens/navigator_bar.dart';
class signup extends StatefulWidget {
const signup({super.key});
@override
State<signup> createState() => _signupState();
}
class _signupState extends State<signup> {
final name = TextEditingController();
final email = TextEditingController();
final password = TextEditingController();
void signUp() async {
try {
await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email.text, password: password.text);
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return NavigatorBar();
}));
} on FirebaseAuthException catch (error) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(error.toString())));
}
}
@override
void dispose() {
name.dispose();
email.dispose();
password.dispose();
super.dispose();
}
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
File? _image;
void selectedImage() async {
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image != null) {
setState(() {
_image = File(image.path);
});
} else {
// Handle the case when the user doesn't pick an image
setState(() {
_image = null;
});
}
}
@override
Widget build(BuildContext context) {
final h = MediaQuery.of(context).size.height;
final w = MediaQuery.of(context).size.width;
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Center(
child: Column(
children: [
SizedBox(
height: h * 0.1,
),
const Text(
'Insta App',
style: TextStyle(
fontSize: 32,
color: Colors.white,
fontWeight: FontWeight.bold),
),
SizedBox(
height: h * 0.05,
),
Stack(
children: [
GestureDetector(
onTap: () {
selectedImage();
},
child: CircleAvatar(
radius: 50,
backgroundImage:
_image == null ? _image = null : FileImage(_image!),
backgroundColor: Colors.white,
child: _image == null
? IconButton(
onPressed: () {
selectedImage();
},
icon: const Icon(
Icons.person,
color: Colors.grey,
size: 50,
))
: null,
),
),
],
),
SizedBox(
height: h * 0.1,
),
Form(
key: formKey,
child: Column(
children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: h * 0.05),
child: TextFormField(
controller: name,
validator: (value) {
if (value!.isEmpty) {
return 'please enter your email';
}
return null;
},
decoration: const InputDecoration(
hintText: 'Name',
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white)),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
)),
),
),
),
SizedBox(
height: h * 0.01,
),
Padding(
padding: EdgeInsets.symmetric(horizontal: h * 0.05),
child: TextFormField(
controller: email,
validator: (value) {
if (value!.isEmpty) {
return 'please enter an email';
}
return null;
},
decoration: const InputDecoration(
hintText: 'Email',
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white)),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
)),
),
),
),
SizedBox(
height: h * 0.01,
),
Padding(
padding: EdgeInsets.symmetric(horizontal: h * 0.05),
child: TextFormField(
controller: password,
validator: (value) {
if (value!.isEmpty) {
return 'please enter a password';
}
return null;
},
decoration: const InputDecoration(
suffixIcon: Icon(Icons.visibility_off),
hintText: 'Password',
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white)),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
)),
),
),
),
SizedBox(
height: h * 0.01,
),
SizedBox(
height: 50,
width: w * 0.7,
child: ElevatedButton(
onPressed: () {
if (formKey.currentState!.validate()) {
formKey.currentState!.save();
}
;
signUp();
},
child: const Text(
'Send',
style: TextStyle(color: Colors.white),
),
),
),
TextButton(
onPressed: () {},
child: const Text('Do you have an account?'))
],
))
],
),
),
),
);
}
}
im trying to sign up by firebase on my app and when i click send it give me this exception , i tried most solutions and my issue still exist
[internal error has occurred. [ API key not valid. Please pass a valid API key. ] flutter]
Planck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.