firebase-database-dotnet
firebase-database-dotnet copied to clipboard
error: Could not parse auth token. (Flutter)
I'm having a class Products, which looks like this:
class Products with ChangeNotifier {
final String authToken;
Products(this.authToken, this._items);
List<Product> _items = [];// data is fetched from server to _items
Future<void> fetchAndSetProducts() async {
print('Executing fetchAndSetProducts ');
print('authToken : '+authToken.toString());
final url =
'https://flutter-project-8c2b1-default-rtdb.firebaseio.com/products.json?auth=$authToken';
try {
final response = await http.get(Uri.parse(url));
final extractedData = json.decode(response.body) as Map<String, dynamic>;
print(extractedData);
if (extractedData == null) {
return;
}
final List<Product> loadedProducts = [];
extractedData.forEach((prodId, prodData) {
loadedProducts.add(Product(
id: prodId,
title: prodData['title'],
description: prodData['description'],
price: prodData['price'],
isFavorite: prodData['isFavorite'],
imageUrl: prodData['imageUrl'],
));
});
_items = loadedProducts;
notifyListeners();
} catch (error) {
print(error);
throw (error);
}
}
}
And my main.dart file Providers section looks like this:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (ctx) => Auth(),
),
ChangeNotifierProxyProvider<Auth, Products>(
create: (context) => Products('', []),
update: (ctx, auth, previousProducts) => Products(
auth.token,
previousProducts == null ? [] : previousProducts.items,
),
),
ChangeNotifierProvider(
create: (ctx) => Cart(),
),
ChangeNotifierProvider(
create: (ctx) => Orders(),
),
],
child: Consumer<Auth>(
builder: (ctx, auth, _) => MaterialApp(
title: 'MyShop',
theme: ThemeData(
primarySwatch: Colors.purple,
accentColor: Colors.deepOrange,
fontFamily: 'Lato',
),
home: auth.isAuth ? ProductsOverviewScreen() : AuthScreen(),
routes: {
ProductDetailScreen.routeName: (ctx) => ProductDetailScreen(),
},
),
),
);
}
}
This is auth.dart file:
import 'dart:convert';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
import '../models/http_exception.dart';
class Auth with ChangeNotifier {
late String _token;
DateTime? _expiryDate;
late String _userId;
bool get isAuth {
// ignore: unnecessary_null_comparison
return token != null;
}
String get token {
// ignore: unnecessary_null_comparison
if (_expiryDate != null &&
_expiryDate!.isAfter(DateTime.now()) &&
// ignore: unnecessary_null_comparison
_token != null) {
return _token;
}
return '';
}
Future<void> _authenticate(
String email, String password, String urlSegment) async {
final url =
'https://identitytoolkit.googleapis.com/v1/accounts:$urlSegment?key=AIzaSyDKCsheQ6YNRyvnFxUPXnSs1k7Sc6qft04';
try {
final response = await http.post(
Uri.parse(url),
body: json.encode(
{
'email': email,
'password': password,
'returnSecureToken': true,
},
),
);
final responseData = json.decode(response.body);
if (responseData['error'] != null) {
throw HttpException(responseData['error']['message']);
}
_token = responseData['idToken'];
print('token inside auth.dart is: ' + _token);
_userId = responseData['localId'];
_expiryDate = DateTime.now().add(
Duration(
seconds: int.parse(
responseData['expiresIn'],
),
),
);
notifyListeners();
} catch (error) {
throw error;
}
}
Future<void> signup(String email, String password) async {
return _authenticate(email, password, 'signUp');
}
Future<void> login(String email, String password) async {
return _authenticate(email, password, 'signInWithPassword');
}
}
while running the app, I get an empty authToken, and the error is: error: Could not parse auth token.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Closing the issue due to inactivity. Feel free to re-open