Click here to Skip to main content
15,944,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have base64 encoded string version of docx file stored in my database and I have to embed into my Flutter app to view the content of the file and I come up with this solution using webview_flutter which apparently I got from ChatGPT

Dart
import 'dart:convert';
    import 'dart:typed_data';
    
    import 'package:flutter/material.dart';
    import 'package:webview_flutter/webview_flutter.dart';
    
    class ExternalViewer extends StatefulWidget {
    
      final Uint8List fileData;
    
      const ExternalViewer({Key? key, required this.fileData}) : super(key: key);
    
      @override
      _ExternalViewerState createState() => _ExternalViewerState();
    }
    
    class _ExternalViewerState extends State<ExternalViewer> {
    
      late WebViewController webController;
      late String base64Data;
    
      @override
      void initState() {
        super.initState();
        base64Data = base64.encode(widget.fileData);
        webController = WebViewController()..setJavaScriptMode(JavaScriptMode.unrestricted);
        loadDocument();
      }
    
      void loadDocument() async {
        final url = 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,$base64Data';
        await webController.loadRequest(Uri.parse(url));
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: WebViewWidget(
            controller: webController,
          ),
        );
      }
    }


However it shows blank white screen instead and sometimes the app just crashed while trying to load the file using this method. What are the mistakes that cause this to happen? If my current solution is not valid what are the alternative solutions? I accept using third party library suggestions but I cannot find any package that allow viewing docx file from encoded base64 string.

Flutter version: 3.10.2

What I have tried:

I tried to convert the DOCX file to PDF but the conversion failed.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900