0

I'm, making a pdf reader with asset pdf files and my app only returns the last file. upon clicking other files it gives an error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)\] Unhandled Exception: Unable to load asset: assets/books/Masud Rana - Durgom Durgo.pdf

and printing the asset file paths only printed this:

[assets/books/Masud Rana - Durgom Durgo.pdf

here is the code:

import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter/material.dart';
import '../api/pdf_api.dart';
import 'pdf_viewer.dart';
import 'package:path/path.dart' as path;

class AssetBooks extends StatefulWidget {
  const AssetBooks({Key? key}) : super(key: key);

  @override
  State createState() => _AssetBooksState();
}

class _AssetBooksState extends State {
  List assetFilePaths = [];

  @override
  void initState() {
    super.initState();
    _getAssetFilePaths();
  }

  void _getAssetFilePaths() async {
    String fileString = await rootBundle.loadString('assets/files.txt');
    List filePaths = fileString.split('\n');
    setState(() {
      assetFilePaths.addAll(filePaths);
    });
    print(assetFilePaths);
  }

  @override
  Widget build(BuildContext context) {
    void openPDF(BuildContext context, file) => Navigator.of(context).push(
        MaterialPageRoute(builder: (context) => PDFViewerPage(file: file)));

    return Scaffold(
        appBar: AppBar(
          actions: [
            ElevatedButton(onPressed: (){print(assetFilePaths);}, child: Text("hi"))
          ],
          title: const Text("Classic Books"),
        ),
        body: Padding(
          padding: const EdgeInsets.all(20.0),
          child: Container(

              // color: Colors.amber,
              child: GridView.builder(
            physics: const BouncingScrollPhysics(),
            shrinkWrap: true,
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              crossAxisSpacing: 20.0,
              mainAxisSpacing: 20.0,
            ),
            itemCount: assetFilePaths.length,
            itemBuilder: (context, index) {
              return assetFilePaths == null
                  ? Center(child: CircularProgressIndicator())
                  : Container(
                      decoration: BoxDecoration(
                          borderRadius:
                              BorderRadius.circular(10), // radius of 10
                          color: Colors.green // green as background color

                          ),
                      child: ElevatedButton(
                          onPressed: () async {
                            print(assetFilePaths);
                            final path = assetFilePaths[index];
                            final file = await PDFApi.loadAsset(path);

                            openPDF(context, file);
                          },
                          child: Text(path.basenameWithoutExtension(assetFilePaths[index]))),
                    );
            },
          )),
        ));
  }
}

here is file.txt:

assets/books/Masud Rana - Durgom Durgo.pdf 
assets/books/advs.pdf 
assets/books/Flutter Roadmap.pdf 
assets/books/Mein Kampf Adolf Hitler.pdf 
assets/books/The Da Vinci Code.pdf

I expected clicking the button will open the pdf reader. but this just opens on the last file "The da vinci code". I've tried flutter clean but the result is same.

  • can you try the accepted answer of this post to get list of files ? https://stackoverflow.com/questions/56544200/flutter-how-to-get-a-list-of-names-of-all-images-in-assets-directory – Clevino Alrin Feb 11 '23 at 17:16

1 Answers1

0

Have you added your assets to the pub?

Inside pubspec.yaml:

assets:
    - assets/
    - assets/books/