I am using MySQL NodeJS and flutter web for my application and wanted to view a pdf in it. I don’t want to use links to view the pdfs I have stored it in a large binary object format in my MySQL database
here is my code up till now
import 'package:digital_menu/constants.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:typed_data';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
class OwnerPage extends StatefulWidget {
@override
_OwnerPageState createState() => _OwnerPageState();
}
class _OwnerPageState extends State<OwnerPage> {
String restaurantName = '';
late int ownerId;
String pdfBase64 = '';
Uint8List? pdfBytes;
@override
void didChangeDependencies() {
super.didChangeDependencies();
final args =
ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
ownerId = args['ownerId'];
fetchRestaurantDetails(ownerId);
}
Future<void> fetchRestaurantDetails(int ownerId) async {
try {
var url = Uri.parse(
'http://localhost:5000/fetch_restaurant_details?ownerId=$ownerId');
var response = await http.get(url);
if (response.statusCode == 200) {
var data = jsonDecode(response.body);
setState(() {
restaurantName = data['restaurantName'];
pdfBase64 = data['pdfBase64'];
//print("PDF: $pdfBase64");
pdfBytes = base64Decode(pdfBase64);
print("pdf bytes: $pdfBytes");
});
} else {
setState(() {
restaurantName = 'Error fetching restaurant details';
pdfBase64 = '';
});
}
} catch (e) {
setState(() {
restaurantName = 'Failed to connect to server';
pdfBase64 = '';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Owner ID: $ownerId'),
SizedBox(height: 20),
Text('Restaurant Name: $restaurantName'),
SizedBox(height: 20),
if (pdfBytes != null)
Container(
decoration: BoxDecoration(
color: C.baseColorDark,
),
child: SfPdfViewer.memory(pdfBytes!),
),
],
),
),
),
);
}
}
i have used syncfusion flutter pdf viewer package with the following version ^26.1.38
New contributor
random dude is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.