graphql_flutter Error: The non-abstract class 'GraphQLWebSocketChannel' is missing implementations - flutter

I am trying to use graphql_flutter (https://pub.dev/packages/graphql_flutter) for my MVVM architecture. (https://stacked.filledstacks.com/docs/getting-started/overview) I got this error below from the package graphql_flutter when I try to run my code
`../../Developer/flutter/.pub-cache/hosted/pub.dartlang.org/graphql-5.1.2/lib/src/links/websocket_link/websocket_client.dart:577:7: Error: The non-abstract class 'GraphQLWebSocketChannel' is missing implementations for these members:
WebSocketChannel.ready
Try to either
provide an implementation,
inherit an implementation from a superclass or mixin,
mark the class as abstract, or
provide a 'noSuchMethod' implementation.
class GraphQLWebSocketChannel extends StreamChannelMixin<dynamic>
^^^^^^^^^^^^^^^^^^^^^^^
../../Developer/flutter/.pub-cache/hosted/pub.dartlang.org/web_socket_channel-2.3.0/lib/src/channel.dart:56:22: Context: 'WebSocketChannel.ready' is defined here.
final Future<void> ready = Future.value();`
This is my code in the main.dart file. I am getting the error as long as I have imported the package.
import 'package:flutter/material.dart';
import 'package:testing/app/app.locator.dart';
import 'package:testing/ui/common/app_colors.dart';
import 'package:testing/ui/setup/setup_bottom_sheet_ui.dart';
import 'package:testing/ui/setup/setup_dialog_ui.dart';
import 'package:stacked_services/stacked_services.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'app/app.router.dart';
void main() {
setupLocator();
setupDialogUi();
setupBottomSheetUi();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
HttpLink httpLink = HttpLink("https://api.github.com/graphql");
AuthLink authLink = AuthLink(
getToken: () async => 'Bearer PERSONAL_ACCESS_TOKEN',
);
Link link = authLink.concat(httpLink);
ValueNotifier<GraphQLClient> qlClient = ValueNotifier(
GraphQLClient(
link: link,
// The default store is the InMemoryStore, which does NOT persist to disk
cache: GraphQLCache(store: HiveStore()),
),
);
return GraphQLProvider(
client: qlClient,
child: MaterialApp(
title: 'Flutter Demo',
theme: Theme.of(context).copyWith(
primaryColor: kcBackgroundColor,
focusColor: kcPrimaryColor,
textTheme: Theme.of(context).textTheme.apply(
bodyColor: Colors.black,
),
),
initialRoute: Routes.startupView,
onGenerateRoute: StackedRouter().onGenerateRoute,
navigatorKey: StackedService.navigatorKey,
navigatorObservers: [
StackedService.routeObserver,
],
));
}
}

I got this error yesterday. I added it to the pubspec.yaml wrote the following lines and it worked for me
dependency_overrides:
web_socket_channel: 2.2.0
link - https://github.com/flutter/cocoon/pull/2405 (go to the section "Files changed")

Related

GetIt package - Object/factory not registered inside GetIt

I am using the GetIt package in my Flutter project to manage dependencies. However, I'm facing an issue where the package is throwing an _AssertionError with the following message:
'package:get_it/get_it_impl.dart': Failed assertion: line 372 pos 7:
'instanceFactory != null': Object/factory with type Client is not
registered inside GetIt. (Did you accidentally do GetIt
sl=GetIt.instance(); instead of GetIt sl=GetIt.instance; Did you
forget to register it?)
I have tried to await the initialization of the dependencies inside the main function before running the app, but the error persists. I have no clue how to debug this issue.
Can anyone please guide me on how to resolve this error and properly register my dependencies with GetIt? Any help would be appreciated. Thank you.
dependencies:
bloc: ^8.1.1
flutter_bloc: ^8.1.2
get_it: ^7.2.0
code:
//main.dart
import 'package:bloc_app/theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'home_feature/home_screen.dart';
import 'home_feature/application/bloc/api_request_bloc.dart';
import 'package:bloc_app/injection.dart' as di;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await di.init();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: AppTheme.ligthTheme,
darkTheme: AppTheme.darkTheme,
themeMode: ThemeMode.system,
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: BlocProvider(
create: (context) => di.sl<ApiRequestBloc>(),
child: const MyHomePage(title: 'Bloc App'),
),
);
}
}
The Get_it instance is created globaly here.
//injection.dart
import 'package:bloc_app/home_feature/application/bloc/api_request_bloc.dart';
import 'package:bloc_app/home_feature/domain/repositories/advicer_repository.dart';
import 'package:bloc_app/home_feature/domain/usecases/advicer_usecases.dart';
import 'package:bloc_app/home_feature/infrastructure/datasources/advicer_remote_datasource.dart';
import 'package:bloc_app/home_feature/infrastructure/repositories/advicer_repository_impl.dart';
import 'package:get_it/get_it.dart';
import 'package:http/http.dart' as http;
final sl = GetIt.instance;
Future<void> init() async {
// Blocs
sl.registerFactory(() => ApiRequestBloc(usecases: sl()));
//Usecases
sl.registerLazySingleton(() => AdvicerUseCases(advicerRepository: sl()));
//Repositories
sl.registerLazySingleton<AdvicerRepository>(
() => AdvicerRepositoryImpl(advicerRemoteDataSource: sl()));
//Datasources
sl.registerLazySingleton<AdvicerRemoteDataSource>(
() => AdvicerRemoteDataSourceImpl(client: sl()));
//Extern
sl.registerLazySingleton(() => http.Client);
}
The used bloc that get used inside the main.dart
//api_request_bloc.dart
import 'package:bloc/bloc.dart';
import 'package:bloc_app/home_feature/domain/entities/advice_entity.dart';
import 'package:bloc_app/home_feature/domain/failures/failures.dart';
import 'package:bloc_app/home_feature/domain/usecases/advicer_usecases.dart';
import 'package:dartz/dartz.dart';
// ignore: depend_on_referenced_packages
import 'package:meta/meta.dart';
part './api_request_event.dart';
part './api_request_state.dart';
class ApiRequestBloc extends Bloc<ApiRequestEvent, ApiRequestState> {
final AdvicerUseCases usecases;
ApiRequestBloc({required this.usecases}) : super(ApiRequestInitial()) {
on<ApiRequestEvent>((event, emit) async {
emit(ApiRequestLoading());
Either<AdviceEntity, Failure> adviceOrFailure =
await usecases.getAdviceUsecase();
//If usecase gives error than state retunres falure otherwise the advice get shown
adviceOrFailure.fold(
(advice) => emit(ApiRequestLoaded(advice: advice.advice)),
(failure) => emit(ApiRequestError(
error: _mapFailureToError(failure),
)),
);
});
}
String _mapFailureToError(Failure failure) {
switch (failure.runtimeType) {
case ServerFailure:
return 'Error: ${failure.runtimeType} ~ could not communicate with the server.';
case GeneralFailure:
return 'Error: ${failure.runtimeType} ~ Could not define error.';
default:
return 'Error: ${failure.runtimeType} ~ Could not define error.';
}
}
}
You are missing () while registering http.Client in file injection.dart
//Extern
sl.registerLazySingleton(() => http.Client());

Google OAuth 2.0 failing with Error 400: invalid_request for some client_id, Can't Sign in because 'App' sent an invalid request

I have an Flutter app that uses googleapis_auth 1.3.1 and googleapis 9.2.0 .
What I have done:
enabled the Google Calender API
connect Flutter Project to Firebase
and set up with basic template.
But I am getting the following error:
Here is my code:
`
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:googleapis/calendar/v3.dart' as cal;
import 'package:url_launcher/url_launcher.dart';
import 'gCalender/calendar_client.dart';
import 'secert.dart';
import 'package:googleapis_auth/auth_io.dart';
void main() async {
//firebase initialize
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
//google apis init
var clientID = ClientId(Secret.getId(),"");
var scopes = [cal.CalendarApi.calendarEventsScope];
await clientViaUserConsent(clientID, scopes, prompt).then((AuthClient client) async {
CalendarClient.calendar = cal.CalendarApi(client);
});
runApp(const MyApp());
}
void prompt(String url) async {
if (!await launchUrl(Uri.parse(url))) {
throw 'Could not launch $url';
}
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Social Login',
theme: theme(),
debugShowCheckedModeBanner: false,
initialRoute: SplashScreen.routeName,
// home: const SplashScreen(),
routes: routes,
);
}
}
`
I explored the internet but couldn't find any solution.
if you use emulator, try the app in real phone, cuz some times firebase services not working well in the virtual phones

The argument type 'Future<SharedPreferences>' can't be assigned to the parameter type 'SharedPreferences'

I want to access the shared preferences at the application startup and want to use this same object across the entire app by passing it to the classes. I am getting the following error:
The argument type 'Future' can't be assigned to the
parameter type 'SharedPreferences'.
main.dart
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:application/layouts/ScreenOne.dart';
import 'package:application/layouts/ScreenTwo.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
sharedPreferences() async {
return await SharedPreferences.getInstance();
}
final preferences = SharedPreferences.getInstance();
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: (preferences.getInt("login") == 1 ? ScreenOne(preferences) : ScreenTwo(preferences)),
);
}
}
I am unable to resolve this error. Is there anything I am doing wrong or missing? Thanks!!!
First of all, you defined function sharedPreferences() but did not use it later in the code - simply remove it.
Furthermore, based on the documentation SharedPreferences.getInstance() returns Future<SharedPreferences> and not SharedPreferences, hence you get the following error. You can resolve the issue by getting the SharedPreferences instance in the main method and then using constructor injection to provide the preferences object to the MyApp:
Future<void> main() async { // <-- Notice the updated return type and async
final preferences = await SharedPreferences.getInstance(); // <-- Get SharedPreferences instance
runApp(
MyApp(preferences: preferences), // <-- Inject (pass) SharedPreferences object to MyApp
);
}
class MyApp extends StatelessWidget {
final SharedPreferences preferences;
const MyApp({
required this.preferences,
})
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: (preferences.getInt("login") == 1 ? ScreenOne(preferences) : ScreenTwo(preferences)),
);
}
}

flutter getx package not working, showing error

I recently upgraded flutter. After upgrading, when I going to use Get it's showing down error.
without adding get my project work fine.
Codes
import 'package:flutter/material.dart';
import 'package:flutterfire_auth/src/homepage.dart';
import 'package:get/get.dart';
void main() {
return runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter App',
debugShowCheckedModeBanner: false,
home: Homepage(),
);
}
}
errors
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/get-3.15.0/lib/get_navigation/src/extension_navigation.dart:235:37: Error: No named parameter with the name 'shadowThemeOnly'.
final theme = Theme.of(context, shadowThemeOnly: true);
^^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/material/theme.dart:119:20: Context: Found this candidate, but the arguments don't match.
static ThemeData of(BuildContext context) {
^^
add pub dependency extension in vscode "Pubspec Dependency Search" ,it will help in automitacally add "get" dependency

Flutter - auto_route _CustomNavigatorState error

I'm using the auto_route package in order to route my app and until 2 days ago everything worked just fine, until now.
For some reason, I'm getting the following error.
SYSTEM:
flutter: 1.22
dart: 2.10.0
auto_route: ^0.6.7
ERROR:
../../.pub-cache/hosted/pub.dartlang.org/custom_navigator-0.3.0/lib/custom_navigator.dart:60:7: Error: The non-abstract class '_CustomNavigatorState' is missing implementations for these members:
- WidgetsBindingObserver.didPushRouteInformation
Try to either
- provide an implementation,
- inherit an implementation from a superclass or mixin,
- mark the class as abstract, or
- provide a 'noSuchMethod' implementation.
class _CustomNavigatorState extends State<CustomNavigator>
^^^^^^^^^^^^^^^^^^^^^
/opt/flutter/packages/flutter/lib/src/widgets/binding.dart:122:16: Context: 'WidgetsBindingObserver.didPushRouteInformation' is defined here.
Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
main.dart
import 'package:device_simulator/device_simulator.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'Test.gr.dart' as r;
void main() => runApp(MyApp());
const bool debugEnableDeviceSimulator = true;
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
builder: ExtendedNavigator.builder<r.Router>(
router: r.Router(),
builder: (context, extendedNav) => DeviceSimulator(
enable: debugEnableDeviceSimulator,
child:
Scaffold(body: extendedNav, backgroundColor: Colors.red ),
),
),
);
}
}
Also, I should mention, because of some update of flutter I guess, now I need to use r.Router instead of just Router.
Test.dart
import 'package:auto_route/auto_route_annotations.dart';
#MaterialAutoRouter(
routes: <AutoRoute>[],
)
class $Router {}
And also if has any importance here it's the generated file
Test.gr.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
// **************************************************************************
// AutoRouteGenerator
// **************************************************************************
// ignore_for_file: public_member_api_docs
import 'package:auto_route/auto_route.dart';
class Routes {
static const all = <String>{};
}
class Router extends RouterBase {
#override
List<RouteDef> get routes => _routes;
final _routes = <RouteDef>[];
#override
Map<Type, AutoRouteFactory> get pagesMap => _pagesMap;
final _pagesMap = <Type, AutoRouteFactory>{};
}
Do you have any idea what's going one here or how can I fix this? Or if I can't fix this, what can I use instead of the auto_route package which will offer me the same benefice?
i solved this problem with add some function on library CustomNavigator :
Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
return didPushRoute(routeInformation.location);
}
Find the place where the error is generating. Add the following function below there. I did the same and my problem is solved.
Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
return didPushRoute(routeInformation.location);
}