How to download a CSV in Flutter Web keeping a cross-platform code? - flutter

I am generating a csv file from a List<List<dynamic>> named rows as follows ...
String csv = const ListToCsvConverter().convert(rows);
Then, If I'm on a mobile (Android or IOS) I send the file to an email and if I'm on the web I download it to the device using AnchorElement that is part of import 'dart: html' as html as follows:
if (_prefs.platform == 'isWeb')
{
html.AnchorElement(href: "data:text/plain;charset=utf-8,$csv")
..setAttribute("download", "report.csv")
..click();
} else {
//To write csv as a file in a path and send it using FlutterEmailSender
}
It's working right on Web, but when I try to compile in Android or IOS an error appears:
: Error: Not found: 'dart:html' import 'dart:html' as html;
^
: Error: Method not found: 'AnchorElement'.
html.AnchorElement(href: "data:text/plain;charset=utf-8,$csv")
I think It's because dart:html is not supported by IOS and Android, so my questions are:
What other package / function can I use to download a CSV file on the Web, without generating compilation errors (cross-platform)?
As an alternative solution, can I use some command so that the Widget uses dart:html only if it is running in a web environment?

You can use this instead:
import 'package:universal_html/html.dart' as html hide Text;

Related

How to check internet connectivity in 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

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.

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'
}
}

Unsupported operation: _newZLibDeflateFilter, what is this?

This is getting passed back from my exception. This only occurs on web, on ios and android it works perfectly.
Any insight on how to correct this or point me in the right direction will be appreciated.
_newZLibDeflateFilter means that you are using GZipCodec() which related to dart:io library that's currently doesn't support flutter web, in order to decompress your http response you can use archive package
include the package in your YAML file
dependencies:
archive: ^3.3.2
and use GZipDecoder() that's included in the package instead of dart:io GZipCodec()
import 'package:archive/archive_io.dart';
String responseBodyDecompressed = utf8.decode(
GZipDecoder().decodeBytes(response.bodyBytes) //decompression
);

Cannot find symbol import com.facebook.CallbackManager

Currently working on a react app which uses the Facebook SDK. It seems to brake on the import of com.facebook.CallbackManager with the following error:
/android/app/src/main/java/com/phonebook/theredcorner/MainApplication.java:5: error: cannot find symbol import com.facebook.CallbackManager;
I've tried many suggestions online but it all doesn't seem to work. Anyone recently got this error and knows how to solve it?
I'm importing it in my MainApplication.java as follows
package com.phonebook.theredcorner;
import android.app.Application;
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;
import com.facebook.reactnative.androidsdk.FBSDKPackage;
import com.facebook.appevents.AppEventsLogger;
Furthermore I've followed all steps on the Facebook getting started page to implement the Facebook SDK.
The Facebook SDK was published as an independent module to Maven. Include the dependency in the app/build.gradle file.
dependencies {
// Facebook Core only (Analytics)
implementation 'com.facebook.android:facebook-core:5.0.0'
}
You may also need to add the following to your project/build.gradle file.
buildscript {
repositories {
mavenCentral()
}
}
If the error persists after installation, make sure the file is in the next path.
facebook-android-sdk/facebook-core/src/main/java/com/facebook/CallbackManager.java