'setup' is deprecated and shouldn't be used. Use PurchasesConfiguration - android

How can i fix this and make it not deprecated
import 'package:purchases_flutter/purchases_flutter.dart';
class PurchaseApi{
static const _apiKey = '';
static Future init() async{
await Purchases.setDebugLogsEnabled(true);
await Purchases.setup(_apiKey);
}
static Future<List<Offering>> fetchOffers() async {
try{
final offerings = await Purchases.getOfferings();
final current = offerings.current;
return current == null ? [] : [current];
} on PlatformException catch (e) {
return [];
}
}
}
I already changed the firt on to await Purchases.setLogLevel(true as LogLevel); But when i change the setup one i get an error. The error is The method 'PurchasesConfiguration' isn't defined for the type 'Purchases'. I already tried to import'package:purchases_flutter/models/purchases_configuration.dart';

When you hover over the deprecated setup method, you have a hint.
You need to replace this:
await Purchases.setup(_apiKey);
to this:
PurchasesConfiguration(_apiKey);

Related

Dart Flutter "Non-nullable instance field '_isSigningIn' must be initialized." Error

I'm trying to do a Google Auth operation with Dart Flutter.
I made the exact same code as in the video I watched. While it doesn't give an error in the video, it gives me an error.
My codes:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
class GoogleSignInProvider extends ChangeNotifier {
final googleSignIn = GoogleSignIn();
bool _isSigningIn;
GoogleSignInProvider() {
_isSigningIn = false;
}
bool get isSigningIn => _isSigningIn;
set isSigningIn(bool isSigningIn) {
_isSigningIn = isSigningIn;
notifyListeners();
}
Future login() async {
isSigningIn = true;
final user = await googleSignIn.signIn();
if (user == null) {
isSigningIn = false;
return;
} else {
final googleAuth = await user.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
isSigningIn = false;
}
}
void logout() async {
await googleSignIn.disconnect();
FirebaseAuth.instance.signOut();
}
}
Error:
GoogleSignInProvider GoogleSignInProvider()
package:todolist/google_sign_in.dart
Non-nullable instance field '_isSigningIn' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
What is the problem? How can I solve it? I thank you in advance for the help.
because your value is not nullable, it requires a default value
bool _isSigningIn; //non-nullable variable should be inialized
bool? _isSigningIn; //nullable variable and does not requires initialization
If you make your variable nullable you can leave it uninitialized, in other case, you should either initialize it when you define it
bool _isSigningIn = false;
or inside your constructor with a value, like this:
GoogleSignInProvider(this._isSigningIn);
There is one more option, you can also give a default value:
put your variable in [] and give a default value, it will be positional argument but it won't be required
See the example below:
class GoogleSignInProvider extends ChangeNotifier {
final googleSignIn = GoogleSignIn();
bool _isSigningIn;
GoogleSignInProvider([this._isSigningIn = false]);
}
Make the _isSigningIn either nullable or intitalize it. i.e
class GoogleSignInProvider extends ChangeNotifier {
final googleSignIn = GoogleSignIn();
bool? _isSigningIn; // Note the ? (question mark).
or
class GoogleSignInProvider extends ChangeNotifier {
final googleSignIn = GoogleSignIn();
bool _isSigningIn = false;
You are getting this error because dart now support null-safety and it's on by default. While the older version of dart don't.

GetStorage always returns null in flutter

Code
print("Before : ${GetStorage().read("XXX")}");
GetStorage().write("XXX", 1);
print("After : ${GetStorage().read("XXX")}");
This is my Code. Every time I run the App, the Output is
Before : null
After : 1
Why is the storage data getting cleared everytime I restart the App? I thought this was an alternative to SharedPreference which works just fine. Have I missed something?
Before anything, initialize the package, normally I do this on main.dart
main() async {
await GetStorage.init();
}
Create an instance from GetStorage, I always put a name on the box, if not it will put "GetStorage" by default. It needs to have a name so it can retrieve your data.
GetStorage getStorage = GetStorage('myData');
After that you can write and retrieve data from it, I recommend you to "await" all reads and writes.
await getStorage.write('XXX', 1);
var a = await getStorage.read('XXX');
print(a); /// 1
I recommend you to put a name on the box according to what you are storing.
You should await for GetStorage.init().
void main() async {
await GetStorage.init();
print("Before : ${GetStorage().read("XXX")}");
GetStorage().write("XXX", 1);
print("After : ${GetStorage().read("XXX")}");
}
final _userBox = () => GetStorage('User');
class UserPref {
void call(){
_userBox.call()..initStorage;
}
dynamic setValueInt(String key, int value) {
return 0.val(key, getBox: _userBox).val = value;
}
String setValue(String key, String value) {
return ''.val(key, getBox: _userBox).val = value;
}
dynamic getValueInt(String key) {
return (-1).val(key,getBox: _userBox).val;
}
dynamic getValue(String key) {
return ''.val(key,getBox: _userBox).val;
}
void setUser(User user) {
''.val('uname', getBox: _userBox).val = user.uname ?? '';
(-1).val('gender', getBox: _userBox).val = user.gender ?? -1;
''.val('born', getBox: _userBox).val = user.born.toString();
true.val('enabled', getBox: _userBox).val = user.enabled ?? true;
}
User getUser() {
final String? uname = ''.val('uname',getBox: _userBox).val;
final int? gender = (-1).val('gender',getBox: _userBox).val;
final DateTime? born = ''.val('born',getBox: _userBox).val == '' ? null : DateTime.parse(''.val('born',getBox: _userBox).val);
final bool? enabled = true.val('enabled',getBox: _userBox).val;
return User(
uname: uname,
gender: gender,
born: born,
enabled: enabled,
);
}
}
///INIT:
#override
void initState() {
//The init function must be written separately from the read/write function due to being asynchronous.
UserPref().call();
}
//OR
Future<void> main() async {
//await GetStorage.init();
UserPref().call();
}
///USAGE:
class MyStatefulWidget extends StatefulWidget {
final Users prefUser = UserPref().getUser();
...
}
//OR
#override
Widget build(BuildContext context) {
final Users prefUser = UserPref().getUser();
return ...;
}

Flutter “this function has a return type of void and I want a return type of map”

I am getting this error:This expression has a type of 'void' so its value can't be used.
Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void.
code:
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_hot/angel_hot.dart';
import 'package:logging/logging.dart';
import 'dart:async';
import 'package:mongo_dart/mongo_dart.dart';
main() async {
var hot = HotReloader(createServer, ['main.dart']);
await hot.startServer('127.0.0.1', 3000);
}
Future<Angel> createServer() async {
var app = Angel();
app.logger = Logger('Log')..onRecord.listen((event) => print(event));
print('start server..');
Db db = Db('mongodb://localhost:27017/wearina');
await db.open();
print('connected to ${db.databaseName}');
DbCollection userscoll = DbCollection(db, 'users');
print('${userscoll.collectionName}');
app.post('/signup', (req, res) async {
var body = await req.parseBody(); //// parseBody => Future<void> , I want => Future<Map> ):
var name = body['name'];
var lastname = body['lastname'];
var email = body['email'];
var phone = body['phone'];
var pass = body['pass'];
});
return app;
}
I don't understand what this is. I am new to flutter. This is my first app. Can someone please help me with this.
It seems that parseBody does not return a map of the pasred body. It just makes sure the body is parsed and you can access it from the req.bodyAsMap property. So your line should be:
await req.parseBody();
var body = req.bodyAsMap;

NoSuchMethodError: The getter 'path' was called on null. Receiver: null Tried calling: path

When I pass pdf URL value to this its getting error with the built-in keyword "path" and it seems to be null?
loadPdf(String pdfPath) async {
setState(() => _isLoading = true);
var fileName = pdfPath.split('/').last;
var localFileUrl = (await Directory(CacheManager.getInstance().appDocumentDir.path +'/'+"realpro"+"/").create(recursive: true)).path +fileName;
if (await CacheManager.getInstance().checkFileExist(localFileUrl)) {
document = await PDFDocument.fromAsset(localFileUrl);
print(document);
setState(() {
_isLoading = false;
});
} else {
document = await PDFDocument.fromURL(pdfPath);
print(document);
setState(() {
_isLoading = false;
});
}
}
The getter 'path' was called on null.
The error means that the object for which you are writing object.path is null. You can use ?. operator like this: object?.something which is equivalent to:
object!=null ? object.something : null

Flutter how to get a Future<bool> to a normal bool type

Hi I'm trying to get a Future to be used as a normal boolean, how do I use this function as the determiner for a normal boolean without it giving me an incorrect type error?
Future<bool> checkIfOnAnyChats() async {
FirebaseUser user = await _auth.currentUser();
final QuerySnapshot result = await _firestore
.collection('chats')
.where('members', arrayContains: _username)
.getDocuments();
final List<DocumentSnapshot> documents = result.documents;
if(documents.length > 0) {
return Future<bool>.value(true);
}else{
return Future<bool>.value(false);
}
}
How do I apply it to a normal type boolean and not get this error? Thanks.
you don't need to convert bool into future, as you are in async method it will return future only.
you can get that value in initstate, you can not get value outside any method.
bool _isInChat;
#override
void initState() {
super.initState();
CheckIfOnAnyChats().then((value){
SetState((){
_isInChat = value;
});
});
}

Categories

Resources