Flutter replace all HTML Codes - flutter

I'm getting json data from a wordpress website. Some of the content I fetch can have html codes like &#8217 or &#8230 and I want to convert this to normal caracter like ' or ...
I can't seem to understand what function to use from the dart API to do this.
I've been able to convert them 1 by 1 using RegExp but I'm sure there is a better way to do this.
new RegExp(r'…'),'...'))),
Thanks for the help.

There's a library called html_unescape that can do that, you can find it here.
The steps to get it working are described there but as a quick reference:
1.- Include the dependency on your pubspec.yaml
dependencies:
html_unescape: ^1.0.1+3
flutter:
sdk: flutter
2.- Install the dependeny
Click on Packagets get while on the pubsec.yaml file
3.- Import it and use it:
import 'package:html_unescape/html_unescape.dart';
void main() {
var unescape = HtmlUnescape();
// prints ’ and …
print(unescape.convert('’ and …'));
}
//EDIT
Just as extra info, this is the google query I used to find this:
"convert html entities string flutter"

Related

How to ignore package when building flutter project for web?

I have a project which uses flutter_libserialport library on macOS.
I am modifying it to work on web however this library does not work on web.
I am building a web implementation using navigator.serial in javascript which works fine.
However when I attempt to build the project for web I get the following error
/opt/homebrew/Caskroom/flutter/2.2.3/flutter/.pub-cache/hosted/pub.dartlang.org/libserialport-0.2.0+3/lib/src/config.dart:25:8: Error: Not found: 'dart:ffi'
import 'dart:ffi' as ffi;
This makes sense since FFI is not available on web.
But I don't even need the libserialport library on web any way.
How can I get flutter to ignore it?
I tried this however it doesn't contain information on how to exclude a package.
It also does not contain information on how to ignore it specifically for web. It seems to just ignore it in general.
Maybe you should guard your usages of libserialport with the kIsWeb predicate like following:
if(!kIsWeb){
// libserialport code execution here
}
I searched a lot as well and didn't find a way you can do that, I think this should be handled by the package itself not the package's users like in path_provider for instance.
As a workaround I have created a dummy libserialport's SerialPort class for web only as follows:
dummy_serialport.dart:
class SerialPort {
final String name;
static List<String> availablePorts = ['dummy'];
static SerialPortError? lastError;
SerialPort(this.name);
bool openReadWrite() {
return false;
}
}
class SerialPortError {}
// add more properties and functions as needed
main.dart:
import 'package:libserialport/libserialport.dart'
if (dart.library.html) './dummy_serialport.dart'
if (dart.library.io) 'package:libserialport/libserialport.dart';
....
if (!kIsWeb) {
final name = SerialPort.availablePorts.first;
final port = SerialPort(name);
if (!port.openReadWrite()) {
print(SerialPort.lastError);
exit(-1);
}
}
....
....
It's bad, I know :( but it works! maybe you can contact the package author to get more insight and if opening a PR where the interfaces are separated from the FFI implementation so that importing the classes wouldn't break web or something.

Remove hash symbol ' # ' from Flutter web navigation

I want to create one simple web application with Flutter web but after I create some simple application with this document I faced with some issue on routing address it automatically add one hash '#' symbol to URL on the address bar, I want to know how I can remove this sign from URL, In fact, right now I see something like this on browser address-bar : http://[::1]:54587/#/register but I want to achieve to something like this http://[::1]:54587/register.
Configuring the URL strategy on the web
Include the flutter_web_plugins package and call the setUrlStrategy function before your app runs:
dependencies:
flutter_web_plugins:
sdk: flutter
Create a lib/configure_nonweb.dart file with the following code:
void configureApp() {
// No-op.
}
Create a lib/configure_web.dart file with the following code:
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
void configureApp() {
setUrlStrategy(PathUrlStrategy());
}
Open lib/main.dart and conditionally import configure_web.dart when the html package is available, or configure_nonweb.dart when it isn’t:
import 'package:flutter/material.dart';
import 'configure_nonweb.dart' if (dart.library.html) 'configure_web.dart';
void main() {
configureApp();
runApp(MyApp());
}
If your only concern is for routing, you can check out my answer here: https://stackoverflow.com/a/63042805/210417
Basically it just splits the current URL into a List and then removes the empty ones caused by the hash tag.

how to read V Card file(contact number,name etc) in flutter without use string split

i try to read contacts number and name from this V Card file.
i don't know,what is the proper any way to read .
i use string split method.
but any other proper way to read this, please suggest
( V Card file image show here)
You can user VCard parser from https://pub.dev/packages/vcard_parser
add to pubspec
dependencies:
vcard_parser: ^0.1.6
in code import
import 'package:vcard_parser/vcard_parser.dart';
Now
Map<String, Object> map = VcardParser(String VCardcontent).parse() ;

Google Places API in Flutter

I am working on google places API with Flutter. I am working by referring the example. But I got errors for google places API classes as
Eg:
Undefined class 'GoogleMapsPlaces'.
Try changing the name to the name of an existing class, or creating a class with the name
I imported the flutter_google_places in my dart file as:
import 'package:flutter_google_places/flutter_google_places.dart'; But still I got the error for all classes.
Using flutter_google_places version 0.2.3.
GoogleMapPlaces is available on different library, not in flutter_google_places...
it's available on https://pub.dev/packages/google_maps_webservice
you can find another package for google place google_place
var googlePlace = GooglePlace("Your-Key");
var result = await googlePlace.autocomplete.get("1600 Amphitheatre");
You need to import
import 'package:google_maps_webservice/places.dart'; in your main.dart.

How can I read and Write Files in Flutter Web?

I am working on flutter-web, I would like to do file operations(read and write) in flutter-web.
For Android and IOS, I was using path_provider dependency. But in Flutter-web, it is not supported.
Can anyone help me with this?
The accepted answer is not completely right. Yes, dart:io is not available on the web, but it is still possible to read files. You can select a file through the system's file picker and read it afterward.
An easy option to "write" a file is to send the user an auto-download.
Read:
Use this library to select a file: pub.dev/packages/file_picker (Web migration guide)
import 'dart:html' as webFile;
import 'package:file_picker_web/file_picker_web.dart' as webPicker;
if (kIsWeb) {
final webFile.File file = await webPicker.FilePicker.getFile(
allowedExtensions: ['pd'],
type: FileType.custom,
);
final reader = webFile.FileReader();
reader.readAsText(file);
await reader.onLoad.first;
String data = reader.result;
}
Write (a.k.a download):
import 'dart:html' as webFile;
if (kIsWeb) {
var blob = webFile.Blob(["data"], 'text/plain', 'native');
var anchorElement = webFile.AnchorElement(
href: webFile.Url.createObjectUrlFromBlob(blob).toString(),
)..setAttribute("download", "data.txt")..click();
}
in the dart docs it has been explained ... dart:io is not available to web build ... it means that you can not directly access to system files in flutter web . the build will fail .. you need to remove the code in order to run it
this is a security thing .. js doesn't allow it too .. imagine a website that can read all your system files if you visit it .. that's why things like path reading or file reading is not allowed .. it goes same for flutter
hope you have your answer
I created a package specifically for this before Flutter web was a thing. https://pub.dev/packages/async_resource
It looks at things as either a network resource (usually over HTTP), or a local resource such as a file, local storage, service worker cache, or shared preferences.
It isn't the easiest thing in the world but it works.
What about "dart:indexed_db library", You can store structured data, such as images, arrays, and maps using IndexedDB. "Window.localStorage" can only store strings.
dart:indexed_db
Window.localStorage
When it comes specifically to reading a file from disk on web, the easiest way is to use a PickedFile like so:
PickedFile localFile = PickedFile(filePath);
Uint8List bytes = await localFile.readAsBytes();