How to check internet connectivity in Flutter? - flutter

import 'package:internet_connection_checker/internet_connection_checker.dart';
/*Error --> Target of URI doesn't exist: 'package:internet_connection_checker/internet_connection_checker.dart'.
Try creating the file referenced by the URI, or Try using a URI for a file that does exist*/
....
void checkingNetwork() async {
ans = await InternetConnectionChecker().hasConnection;
/*Error -- The method 'InternetConnectionChecker' isn't defined for the type '_WelcomeMessageState'.
Try correcting the name to the name of an existing method, or defining a method named 'InternetConnectionChecker'.*/
}
#override
void initState() {
super.initState();
checkingNetwork();
}
I have this code to check network connectivity but even after adding plugin to pubspec, its giving error. How can I remove this?

If you want to use a package called internet_connection_checker, you have to add it to your pubspec.yaml file.
Then you can run flutter pub get to install the package.
If you have done that, the import line
import 'package:internet_connection_checker/internet_connection_checker.dart';
should compile successfully and the symbol InternetConnectionChecker will be found.

After adding The Package In pubspec.yaml Please Do flutter clean && flutter pub get command to get the packages , Or You Can Use connectivity_plus package . This has listener in it So you Can Track The Internet Connectivity Through The App AllTime

Related

Undefined name 'Provider' in flutter

I am getting an error called "Undefined name 'Provider' in flutter. Here is the code snippet for it:
addData() async {
UserProvider _userProvider = Provider.of(context, listen : false);
await _userProvider.refreshUser();}
Can someone help me figure this out?
You need to import the provider package on the current page
import 'package:provider/provider.dart';
Provider is a powerful State Management package which has to be added to the project and imported in order to use it.
If you haven't added the package, then add it by the following command in the project folder in Terminal/Command Prompt:
flutter pub add provider
Then in the file containing the snippet, import the package at the top like the following:
import 'package:provider/provider.dart';
Check if you have following dependency added in your pubspec.yaml file
dependencies:
provider: ^6.0.3
If not add it or simply run in your terminal
flutter pub add provider
After getting your dependency import the provider package in your page:
import 'package:provider/provider.dart';

Can't run Flutter web on Chrome Device with BluetoothThermalPrinter package

Can't run my flutter web app when blue_thermal_printer package is being used.
Compiler show these errors:
import 'package:blue_thermal_printer/';
^
/D:/All%20Data/Softwares/Latest%20Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/blue_thermal_printer-1.1.8/lib/blue_thermal_printer.dart:189:23:
Error: Type 'Registrar' not found.
static registerWith(Registrar value){
^^^^^^^^^
lib/generated_plugin_registrant.dart:31:3: Error: Getter not found: 'BlueThermalPrinterPlugin'.
BlueThermalPrinterPlugin.registerWith(registrar);
What I have tried until now:
1- Tried to enable flutter web support with this command: flutter create .
2- Tried to create a new class BlueThermalPrinterPlugin in the package file blue_thermal_printer.dart with the following method:
class BlueThermalPrinterPlugin{
static var nameoo ;
static registerWith(Registrar value){
this.nameoo=value;
return nameoo;
}
}
But that doesn't either helps because sdk generated automatic generated_plugin_registrant.dart which produces incomplete import i.e import 'package:blue_thermal_printer/'; causing compile time issue.
I need to run my flutter app on web which was working fine a few days before.
Thanks to the developer of this package whom I contacted personally and he corrected the error in packge. blue_thermal_printer 1.2.0 is working fine and doesn't contain this error anymore.

The method 'getApplicationDocumentsDirectory' isn't defined for the type '_MyAppState'

so a part of my code is
Future<void> _loadModel() async {
final bytes =
await rootBundle.load('assets/deepspeech-0.9.3-models.tflite');
final directory = (await getApplicationDocumentsDirectory()).path;
And i keep getting the error:
The method 'getApplicationDocumentsDirectory' isn't defined for the type '_MyAppState'.
Try correcting the name to the name of an existing method, or defining a method named 'getApplicationDocumentsDirectory'
What should i do? help me please!
You have to install path provider package by running flutter pub add path_provider in your terminal. If you already installed it. check whether you are importing it to your file.
Import this...
import 'package:path_provider/path_provider.dart';
Add path_provider: ^2.0.11 to pubspec.yaml file.
Run Pub Get.
And Import this
import 'package:path_provider/path_provider.dart';

Flutter dio cannot see class Interceptor

I'm try to make custom interceptor but when I wrote this code:
import 'package:dio/dio.dart';
class CustomInterceptors extends Interceptor {
}
Compiler throw error:
Cannot find name Interceptor
I was try to reload VSC, make:
flutter packages get
nothing work. I already install dio package and import it work but VSC don't see class Interceptor at all
flutter clean
delete pubspec.lock
pub get

Flutter How to import backendless_sdk messaging module?

I am following this article
How to Create a Chat App with Backendless SDK for Flutter
import 'package:flutter/material.dart';
import 'package:backendless_sdk/backendless_sdk.dart';
import 'package:backendless_sdk/src/modules/modules.dart';
There is a error:
"Target of URI doesn't exist: 'package:backendless_sdk/src/modules/modules.dart'."
The modules.dart import is required for Backendless.Messaging, but without the import there is an error:
The getter 'Messaging' isn't defined for the type 'Backendless'.
void initListeners() async {
Channel channel = await Backendless.Messaging.subscribe("myChannel");
channel.addMessageListener(onMessageReceived);
}
pub spec.yaml
dependencies:
flutter:
sdk: flutter
backendless_sdk: ^1.1.8
How can I import the Messaging module?
You should change from:
await Backendless.Messaging.subscribe
in to:
await Backendless.messaging.subscribe
^
|
small "m" here
Versions
I checked backendless_sdk: ^0.0.2 (from tutorial) and backendless_sdk: ^1.1.8 (newest one), and in both cases this field was named messaging (lowercase).
Class Backendless
backendless_sdk-1.1.8/lib/src/backendless.dart:
It look like you are missing the installation step.
From the terminal: Run flutter pub get.
OR
From Android Studio/IntelliJ: Click Packages get in the action ribbon
at the top of pubspec.yaml.
From VS Code: Click Get Packages located
in right side of the action ribbon at the top of pubspec.yaml.
I do not see any references to modules.dart in the article you mentioned. You need to import backendless_sdk and also include the socket.io dependency:
dependencies {
implementation ('io.socket:socket.io-client:1.0.0') {
// excluding org.json which is provided by Android
exclude group: 'org.json', module: 'json'
}
}