I am trying to make a listview of items that opens the designated PDF file in an asset folder upon being tapped
void openPDF(BuildContext context, File file) => Navigator.of(context).push(
MaterialPageRoute(builder: (context) => JournalViewerPage(file: file)),
);
onTap: ()async {
final path = 'assets/${journal_list.where((element) => element.fileLocation!)}';
final file = await JournalAPI.loadAsset(path);
openPDF(context, file);
},
However element.fileLocation is throwing an error stated in the title above. Here is the model to my listview
class JournalModel {
final String? journalTitle;
final int? journalReleaseYear;
final String? author;
final String? topic;
final String? fileLocation;
const JournalModel({
this.journalTitle,
this.journalReleaseYear,
this.author,
this.topic,
this.fileLocation,
});
}
And a few samples of the data i've inserted
import 'package:aplikasi_jurnal_mobile/models/journals.dart';
List<JournalModel> journal_list = [
const JournalModel(
journalTitle: 'Pembangunan Sistem Informasi Penjualan Obat Pada Apotek Punung',
journalReleaseYear: 2014,
author: "Tri Utami, Bambang Eka Purnama, Sukadi",
topic: "Medis",
fileLocation: '44-83-2-PB.pdf'
),
const JournalModel(
journalTitle: 'Pembuatan Aplikasi Pembelajaran Bahasa Inggris Pada Handphone dengan J2ME',
journalReleaseYear: 2010,
author: "Yusni Nyura",
topic: "Pendidikan",
fileLocation: '66-204-1-PB.pdf'
),
];
If it helps as well, here is the code to the API
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:aplikasi_jurnal_mobile/models/journals.dart';
import 'package:aplikasi_jurnal_mobile/data/journal_data.dart';
import 'dart:io';
import 'package:path/path.dart';
class JournalAPI {
static Future<File> loadAsset(String path) async{
final data = await rootBundle.load(path);
final bytes = data.buffer.asUint8List();
return _storeFile(path,bytes);
}
static Future<File> _storeFile(String path, List<int> bytes) async {
final filename = basename(path);
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/$filename');
await file.writeAsBytes(bytes, flush: true);
return file;
}
}
I have tried changing fileLocation = 'assets/filename.pdf' to fileLocation = 'filename.pdf'and called it using final path =${journal_list.where((element) => element.fileLocation!)}, but no luck. I also tried changing JournalModel to const whereas previously it wasn't, still nothing.