我想从手机上的文件夹中读取配置文件。但是我设法读取文件的一部分,并且希望能够读取整个文件。关于如何正确执行操作的任何想法?
这是我的代码:
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:ext_storage/ext_storage.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// String _platformVersion = 'Unknown';
String _configFile = 'No Data';
static Future<String> get _getPath async {
final localPath = await ExtStorage.getExternalStoragePublicDirectory(
ExtStorage.DIRECTORY_DOWNLOADS);
return localPath;
}
static Future<File> get _localFile async {
final path = await _getPath;
return File('$path/config_test.txt');
}
static Future<String> readConfig() async {
try {
final file = await _localFile;
// Read the file.
String contents = await file.readAsString();
return contents;
} catch (e) {
// If encountering an error, return 0.
return "error";
}
}
@override
void initState() {
super.initState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
if (await FlutterOpenvpn.init(
localizedDescription: "ExampleVPN",
providerBundleIdentifier:
"com.topfreelancerdeveloper.flutterOpenvpnExample.RunnerExtension",
)) {
await FlutterOpenvpn.lunchVpn(
_configFile,
(isProfileLoaded) => print('isProfileLoaded : $isProfileLoaded'),
(vpnActivated) => print('vpnActivated : $vpnActivated'),
//expireAt: DateTime.now().add(Duration(seconds: 30)),
);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('OpenVPN connect Test'),
),
body: Center(
child: Column(
children: <Widget>[
IconButton(
iconSize: 48.0,
splashColor: Colors.greenAccent,
onPressed: _startBtnVpn,
icon: Icon(Icons.play_arrow)),
IconButton(
iconSize: 48.0,
splashColor: Colors.greenAccent,
icon: Icon(Icons.stop),
onPressed: _stopBtnVPN,
),
IconButton(
iconSize: 48.0,
splashColor: Colors.greenAccent,
icon: Icon(Icons.cloud),
onPressed: () {
readConfig().then((value) {
setState(() {
_configFile = value;
print(_configFile);
});
});
}),
],
),
),
),
);
}
void _startBtnVpn() {
setState(() {
initPlatformState();
});
}
void _stopBtnVPN() {
FlutterOpenvpn.stopVPN();
}
}
调试控制台:
I/flutter (17341): "dev tun\n" +
I/flutter (17341): "proto udp\n" +
I/flutter (17341): "remote public-vpn-255.opengw.net 1195\n" +
I/flutter (17341): ";http-proxy-retry\n" +
I/flutter (17341): ";http-proxy [proxy server] [proxy port]\n" +
I/flutter (17341): "cipher AES-128-CBC\n" +
I/flutter (17341): "auth SHA1\n" +
I/flutter (17341): "resolv-retry infinite\n" +
I/flutter (17341): "nobind\n" +
I/flutter (17341): "persist-key\n" +
I/flutter (17341): "persist-tun\n" +
I/flutter (17341): "client\n" +
I/flutter (17341): "verb 3\n" +
I/flutter (17341): "<ca>\n" +
I/flutter (17341): "-----BEGIN CERTIFICATE-----\n" +
I/flutter (17341): "MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB\n" +
I/flutter (17341): "iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl\n" +
I/flutter (17341): "cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV\n" +
I/flutter (17341): "BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAw\n" +
I/flutter (17341): "MjAxMDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBiDELMAkGA1UEBhMCVVMxEzARBgNV\n" +
I/flutter (17341): "BAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0plcnNleSBDaXR5MR4wHAYDVQQKExVU\n" +
I/flutter (17341): "aGUgVVNFUlR
答案 0 :(得分:0)
问题是print
函数在将输出输出到控制台方面有一个限制,并且不能完全打印长字符串。尝试使用IDE或编辑器设置断点,并检查从文件中读取的值。