How do you inherit FirebaseAuth class in a new class? - flutter

class FireAuth extends FirebaseAuth{
FireAuth({Key? key}) : super.instanceFor(app: app);
...
I want to be able to build my own class based on the FirebaseAuth class
but I keep getting this error:
The generative constructor 'FirebaseAuth FirebaseAuth.instanceFor({required FirebaseApp app})' is expected, but a factory was found.
Try calling a different constructor of the superclass, or making the called constructor not be a factory constructor.

To get around the issue with inheriting the FirebaseAuth. I have used the extension function instead of the extends. So it looks like this
extension FireAuth on FirebaseAuth {
...
no need for a constructor now

Related

Singleton injection with parameter

I do want to use the packages get_it and injectable. They are both wonderful, but I'm facing a issue. Maybe my idea is wrong.
My application has two singletons and I want to use different base urls for different app flavors. So I added a parameter to the singleton constructor with the baseUrl value.
The problem is now, that injectable has only #factoryParam as annotation. The name shows the problem. It's not for singletons.
What will be the best practice for that issue? Should I get rid of constructor injection and inject the baseUrl within the singleton like
var url = get_it.get<Config>();
or is that not the best idea?
It can be achievied in few ways, you can register different strings for different env e.g.
#Environment("dev")
#Named("url") // in case if you injecting other strings
String get devUrl...
#Environment("stage")
#Named("url") // in case if you injecting other strings
String get stageUrl...
[Update with example]
#Environment("dev")
#injectable
class ExampleOne{
final String _url;
ExampleOne(#Named("url") this._url)
}
#Environment("stage")
#injectable
class ExampleTwo{
final String _url;
ExampleTwo(#Named("url") this._url);
}
or just one class:
#injectable
class Example{
final String _url;
Example(#Named("url") this._url);
}

Extending store class in Mobx Flutter

What is the correct way to extend a store class in Mobx in flutter? Wherever I try to put 'extends' i get a compilation error.
class Booking = _Booking with _$Booking;
abstract class _Booking with Store {}
class FlightBooking = _FlightBooking with _$FlightBooking ;
abstract class _FlightBooking with Store {}
You can achieve that by making _FlightBooking extend Booking:
class FlightBooking = _FlightBooking with _$FlightBooking ;
abstract class _FlightBooking extends Booking with Store {}
Store of mobx is a mixin, not a class. Mixins cannot be used to extend. You might want to look more into the documentation.

In Dart, check if a class inherits parent class with type checking

I am try to find a safe way to use Bloc patterns in Flutter with a very strong type checking.
Let's say we have this simple Bloc code:
// States
abstract class CourseState {}
class CourseInitialized extends CourseState {}
// Events
abstract class CourseEvent {}
class CourseFetch extends CourseEvent {}
class CourseLoaded extends CourseEvent {}
// Bloc (somewhere in the Bloc)
final event = CourseFetch();
In dart we can use the is operator to check if the event variable is of type CourseFetch or CourseEvent:
event is CourseFetch // true
event is CourseEvent // true
But nothing forbid me to check if event is of type CourseState or even of type a num, String, etc.
event is CourseInitialized // false
event is String // false
Of course is this case event can't be a String, because it has been initialized with CourseFetch() class.
I am try to find a way to forbid programmers (us) to wrongly write if statement that can never be evaluate to true.
In my case scenario I would like to have a way that stop me to check if a variable event of a certain type state in the IDE and give me a red squiggly line.
if (event is CourseInitialized) {
// the above line should give me a warning, this variable event is not a State
}
Any ideas? Linting tools or syntax that can help?
UPDATE:
I follow RĂ©mi suggestion and try out Freezed, it makes the code simple and add up a safe type checking. Here you can find a complete implementation of Bloc with Freezed and Built Value.
There is no such thing (nor is it possible to know that you checked all the possible cases).
What you can instead do is replace the is operator with a function that does it for you.
One example of this is what the code-generator Freezed does.
Instead of:
abstract class CourseEvent {}
class CourseFetch extends CourseEvent {}
class CourseLoaded extends CourseEvent {}
You would write:
#freezed
abstract class CourseEvent with _$CourseEvent {
factory CourseEvent.fetch() = _Fetch;
factory CourseEvent.loaded() = _Loaded;
}
Which is then used as:
CourseEvent event;
event.when(
fetch: () => print('Fetch event'),
loaded: () => print('Loaded event'),
);

What is the purpose of a factory method in flutter/dart?

I'm following a book that uses a factory method to implement a class that is a singleton.
I understand that the main purpose of this is to have just one instance of the class; But what exactly the keyword "factory" does in flutter?
This is the piece of code that I'm referring:
static final DbHelper _dbHelper = DbHelper._internal();
DbHelper._internal();
factory DbHelper() => _dbHelper;
I supose that the _dbHelper is the single instance that is being create with the _internal named constructor and that the factory method returns this single instance, is that correct? Am I missing something?
You have it correct!
You can read more about factory constructors here:
https://dart.dev/guides/language/language-tour#factory-constructors

flutter: Too many positional arguments: 0 expected, but 1 found

I've been following this NoSQL tutorial which uses BLoC. But when I paste in the code from 'fruit_event.dart' into Visual Studio Code, it gives me an error.
fruit_event.dart:
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
import 'package:sembast_prep/data/fruit.dart';
#immutable
abstract class FruitEvent extends Equatable {
FruitEvent([List props = const []]) : super(props); // error here!
}
class LoadFruits extends FruitEvent {}
class AddRandomFruit extends FruitEvent {}
class UpdateWithRandomFruit extends FruitEvent {
final Fruit updatedFruit;
UpdateWithRandomFruit(this.updatedFruit) : super([updatedFruit]);
}
class DeleteFruit extends FruitEvent {
final Fruit fruit;
DeleteFruit(this.fruit) : super([fruit]);
}
I get this error (Visual Studio Code):
[List<dynamic> props = const []]
Too many positional arguments: 0 expected, but 1 found.
Try removing the extra positional arguments.dart(extra_positional_arguments)
Source code of Equatable class: https://github.com/felangel/equatable/blob/master/lib/src/equatable.dart
It has only one constructor and the constructor accepts no argument/parameter. So you are not allowed to call super(props) from your constructor.
But, in your code you are passing one argument i.e props to the constructor of super class i.e Equatable in your case.
Replace this:
FruitEvent([List props = const []]) : super(props);
With this:
FruitEvent([List props = const []]);
And it won't give you any error.
You may want to refer this question to understand usage of super constructor in dart: How do I call a super constructor in Dart?
Edited:
You should use Equitable class to override your hashcode and
toString.
so for the fields which you want to include in your hashcode and
toString you need to have following override:
/// The [List] of `props` (properties) which will be used to determine whether
/// two [Equatables] are equal.
#override
List<Object> get props => [parameter1, parameter2, parameter3.....];
But as you don't have any fields in your class it doesn't makes much
sense to use Equitable.
Read this comment from the source code:
/// A class that helps implement equality
/// without needing to explicitly override == and [hashCode].
/// Equatables override their own `==` operator and [hashCode] based on their `props`.
const Equatable();
You can refer: https://pub.dev/packages/equatable#-example-tab- to learn about using Equitable class.
I hope this helps, in case of any doubt please comment. If this answer helps you then please accept and up-vote it.