call method
Obtains text from a text file that exists in uri
.
If uri
starts with http://
or https://
, a request is made to the network with GET
to retrieve the text.
In this case, request headers can be added at headers
.
If uri
starts with an absolute path of file://
or /
, a search is performed on the terminal file path to obtain the text within the file if it exists.
(Not available on the Web)
If the file is not found, or uri
starts with resource://
, search for the file in Flutter's assets folder and retrieve the text in the file.
If uri
is empty or the text cannot be retrieved for some reason, defaultValue
is returned.
uri
に存在するテキストファイルからテキストを取得します。
uri
がhttp://
、https://
から始まる場合、ネットワークに対してGET
でリクエストを行いテキストを取得します。
その際headers
でリクエストヘッダを付与することが可能です。
uri
がfile://
、または/
の絶対パスで始まる場合、端末のファイルパスに対して検索を行い、ファイルが存在するときそのファイル内のテキストを取得します。
(Webでは利用できません)
ファイルが見つからない場合、もしくはuri
がresource://
で始まる場合はFlutterのアセットフォルダからファイルを検索し、そのファイル内のテキストを取得します。
uri
が空の場合、もしくはテキストがなにかしらの原因で取得出来なかった場合はdefaultValue
が返されます。
Implementation
@useResult
TextProvider call(
String? uri, {
Map<String, String>? headers,
String defaultValue = "",
}) {
if (uri.isEmpty) {
return TextProvider(defaultValue: defaultValue);
}
try {
uri = "$_prefix${uri!.trimString("/")}";
if (uri.startsWith("http")) {
return NetworkTextProvider(
uri,
headers: headers,
defaultValue: defaultValue,
);
} else if (uri.startsWith("/") || uri.startsWith("file:")) {
return FileTextProvider(
uri.replaceAll(RegExp(r"^file:(//)?"), ""),
defaultValue: defaultValue,
);
} else if (uri.startsWith("document:")) {
return FileTextProvider(
uri.replaceAll(RegExp(r"^document:(//)?"), ""),
defaultValue: defaultValue,
dirType: FileImageDirType.document,
);
} else if (uri.startsWith("temp:")) {
return FileTextProvider(
uri.replaceAll(RegExp(r"^temp:(//)?"), ""),
defaultValue: defaultValue,
dirType: FileImageDirType.temporary,
);
} else if (uri.startsWith("temporary:")) {
return FileTextProvider(
uri.replaceAll(RegExp(r"^temporary:(//)?"), ""),
defaultValue: defaultValue,
dirType: FileImageDirType.temporary,
);
} else if (uri.startsWith("resource:")) {
return AssetTextProvider(
uri.replaceAll(RegExp(r"^resource:(//)?"), ""),
defaultValue: defaultValue,
);
} else {
return AssetTextProvider(
uri,
defaultValue: defaultValue,
);
}
} catch (e) {
debugPrint(e.toString());
return TextProvider(defaultValue: defaultValue);
}
}