大家好,我想从本地文件中恢复Json数据,然后在我的谷歌地图上显示它们。我尝试了很多例子,但都没有成功,请帮帮我这是我的代码
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
import 'dart:convert';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'VIVI ENERGY',
theme: ThemeData(
primarySwatch: Colors.teal,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Set<Marker> allMarkers = {};
GoogleMapController _controller;
Future _future;
Future<String> loadString() async {
await rootBundle.loadString('assets/fileJson/shell.json');
}
@override
void initState() {
// TODO: implement initState
super.initState();
_future = loadString();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: Text("VIVO ENERGY"),
backgroundColor: Colors.teal,
),
drawer: new Drawer(
child: ListView(
children: <Widget>[
new UserAccountsDrawerHeader(
accountName: new Text(""),
),
],
),
),
body: Stack(children: [
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: FutureBuilder(
future: _future,
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData || snapshot.data.documents.isEmpty) {
return Text('changement');
} else {
List<dynamic> parsedJson = jsonDecode(snapshot.data);
allMarkers = parsedJson.map((element) {
var coordonnee = element['Coordonnees_gps'];
var coordonneeF = coordonnee.split(',');
var lat = double.parse(coordonneeF[0]);
var Lng = double.parse(coordonneeF[1]);
return Marker(
markerId: MarkerId(element['Nom']),
position: LatLng(lat, Lng));
}).toSet();
}
return GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(5.316667, -4.033333), zoom: 15),
markers: Set.from(allMarkers),
onMapCreated: mapCreated,
);
}),
)
]));
}
void mapCreated(controller) {
setState(() {
_controller = controller;
});
}
}
我尝试了几种方法,但我仍然无法在本地文件中恢复我的数据,我总是将其作为消息的条件之一"snapshot.hasData";
这是我的pubspec.yaml
name: lecolis
description: Application de livraison par geolocalisation
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
http: ^0.13.1
geolocator : ^7.0.1
google_maps_flutter: ^2.0.2
permission_handler: ^7.0.0
modal_progress_hud: ^0.1.3
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/img/gps.png
- assets/img/icon.png
- assets/img/shell.png
- assets/img/badshell.png
- assets/fileJson/shell.json
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages