gbk2utf8 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 83

gbk2utf8 #

A flutter package to convert gbk to utf-8

目的 #

现在这个版本官方的http还不能支持中文gbk的解析,这个项目就是为了解决这个问题

目前这个库已经成熟,类似flutter原有的utf8编码调用,即为gbk.encode和gbk.decode这两个方法进行转化。 如果还有不懂的地方,可以入群854192563讨论.

使用方法 #

增加依赖


gbk2utf8: ^1.0.0

String转gbk流用以上传服务器

gbk.encode("需要转gbk的中文");

注意转化后的结果是List

gbk流转String

gbk.decode(gbk二进制流,一般是http的response);

例子:解析中文html

编写代码

 void download() async {
    try {
      http.Response response =
          await http.get("http://www.ysts8.com/index_hot.html");
      String data = gbk.decode(response.bodyBytes);
      setState(() {
        _text = data;
      });
    } catch (e) {
      setState(() {
        _text = "网络异常,请检查";
      });
    }
  }

完整代码在

这里

效果:

[1.0.1] #

Better document

[1.0.0] #

Change api to gbk.encode and gbk.decode

[0.0.3] #

  • Format code.

[0.0.2] #

  • upgrade sdk version

[0.0.1] #

  • basic usage

example/lib/main.dart

import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:gbk2utf8/gbk2utf8.dart';
import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _text = "正在下载数据...";

  void download() async {
    try {
      http.Response response =
          await http.get("http://www.ysts8.com/index_hot.html");
      String data = gbk.decode(response.bodyBytes);
      setState(() {
        _text = data;
      });
    } catch (e) {
      setState(() {
        _text = "网络异常,请检查";
      });
    }
  }

  @override
  void initState() {
    download();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new SingleChildScrollView(
        child: new Text(_text),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  gbk2utf8: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:gbk2utf8/gbk2utf8.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
81
Health:
Code health derived from static analysis. [more]
93
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
74
Overall:
Weighted score of the above. [more]
83
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Fix lib/src/convert.dart. (-4.41 points)

Analysis of lib/src/convert.dart reported 9 hints, including:

line 75 col 30: Name non-constant identifiers using lowerCamelCase.

line 82 col 33: Name non-constant identifiers using lowerCamelCase.

line 83 col 7: Name non-constant identifiers using lowerCamelCase.

line 83 col 20: Name non-constant identifiers using lowerCamelCase.

line 83 col 33: Name non-constant identifiers using lowerCamelCase.

Fix lib/src/gbk.dart. (-1.49 points)

Analysis of lib/src/gbk.dart reported 3 hints:

line 1 col 5: Name non-constant identifiers using lowerCamelCase.

line 2 col 5: Name non-constant identifiers using lowerCamelCase.

line 3 col 5: Name non-constant identifiers using lowerCamelCase.

Fix lib/src/unicode.dart. (-1.49 points)

Analysis of lib/src/unicode.dart reported 3 hints:

line 1 col 5: Name non-constant identifiers using lowerCamelCase.

line 2 col 5: Name non-constant identifiers using lowerCamelCase.

line 3 col 5: Name non-constant identifiers using lowerCamelCase.

Format lib/gbk2utf8.dart.

Run flutter format to format lib/gbk2utf8.dart.

Maintenance suggestions

The package description is too short. (-18 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Package is getting outdated. (-7.67 points)

The package was last published 56 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test