The problem is the following:
- I'm making authorization in my app and getting a token for bearer authorization.
- After that, I need to create a WebView and pass the Authorization Bearer in the header
I've tried this, but in doesn't help.
import 'package:flutter/material.dart';
import 'package:skin_technology/core/consts/app_colors.dart';
import 'package:skin_technology/core/session_data/repo/session_data_service.dart';
import 'package:webview_flutter/webview_flutter.dart';
class CatalogWebView extends StatefulWidget {
const CatalogWebView({Key? key}) : super(key: key);
@override
State<CatalogWebView> createState() => _CatalogWebViewState();
}
class _CatalogWebViewState extends State<CatalogWebView> {
late WebViewController _webController;
Future<String?> _getToken() async {
final token = (await SessionDataService.sessionData)?.token;
if (token == null) return null;
return token;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('КАТАЛОГ'),
backgroundColor: AppColors.darkMainColor,
),
body: FutureBuilder<String?>(
future: _getToken(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const Center(child: CircularProgressIndicator());
case ConnectionState.done:
if (snapshot.data == null) return Container();
final headers = {"Authorization": "Bearer ${snapshot.data}"};
const url = 'https://clubskt.ru/client/catalog';
return WebView(
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (controller) async {
_webController = controller;
await _webController.loadUrl(url, headers: headers);
},
onPageStarted: (url) {},
);
default:
return Container();
}
},
),
);
}
}
I tried the packages webview_flutter, flutter_webview_pro, flutter_webview_plugin etc. It still doesn't work.