【问题标题】:BASE64 string to Image in FlutterBASE64 字符串到 Flutter 中的图像
【发布时间】:2019-11-24 05:57:45
【问题描述】:

我尝试将BASE64字符串解码为Uint8List

Uint8List _bytes =
    base64.decode('data:image/jpeg;base64,/9j/4AAQ .........');
Image.memory(_bytes);

但出现错误,(字符错误:)

无效字符(在字符 5 处) data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUSEhMVFRUV...

我怎样才能摆脱这个问题?

【问题讨论】:

  • 你只需要base64.decode数据:从/9j/4AAQ .........'开始

标签: flutter dart


【解决方案1】:

您使用的 URI 包含逗号后的数据,因为它由 RFC-2397 定义。 Dart 的 Uri 类是基于 RFC-3986 的,所以你不能使用它。用逗号分割字符串,取最后一部分:

String uri = 'data:image/gif;base64,...';
Uint8List _bytes = base64.decode(uri.split(',').last);

示例代码对初学者很有用☺️

GestureDetector(
onTap:(){
showDialog(context: context, builder: (context){
var img64 = snapshot.getStudentDetailModel?.courseDetails?.pscPaymetSlip;
final decodestring = base64Decode('$img64'.split(',').last);
Uint8List encodeedimg = decodestring;
  return AlertDialog(
         contentPadding: EdgeInsets.zero,
         content: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                  Container(
                   height:300,
                   decoration: BoxDecoration(
                   image: DecorationImage(
                   fit: BoxFit.cover,
                   image: MemoryImage(encodeedimg))),),],),);
                                      });
                                      },
                   child:Text('View',                                                 textAlign:TextAlign.end),),

【讨论】:

    【解决方案2】:

    你可以这样试试

    final UriData data = Uri.parse(plFile.path!).data!;
    print(data.isBase64);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 2020-02-28
      • 2022-12-16
      相关资源
      最近更新 更多