如何从同一个颤振项目中的不同文件访问另一个类?

时间:2021-06-11 18:07:31

标签: flutter android-studio

大家好,几个月前我刚刚开始学习和研究颤振,我现在正在尝试它。

我想知道是否有一种方法可以让我从 request.dart 访问 main.dart 中的类 Position,以便我在 Uri.parse() 中添加我的 api 的部分 $ {postion.latitude} 和${position.longitude} 不会是 Undefined 并引用 main.dart 文件中的 Position 类。谢谢!

main.dart 文件

'''

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:provider/provider.dart';
import 'package:rider_app/AllScreens/searchScreen.dart';
import 'package:rider_app/AllWidgets/Divider.dart';
import 'package:rider_app/Assistants/assistantMethods.dart';
import 'package:rider_app/DataHandler/appData.dart';

class MainScreen extends StatefulWidget
{
  static const String idScreen = "mainScreen";
  const MainScreen({Key key}) : super(key: key);
  
  @override
  _MainScreenState createState() => _MainScreenState();
}




class _MainScreenState extends State<MainScreen>
{
  Completer<GoogleMapController> _controllerGoogleMap = Completer();
  GoogleMapController newGoogleMapController;

  GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();

  Position currentPosition;
  var geoLocator = Geolocator();
  double bottomPaddingOfMap=0;

  void locatePosition() async
  {
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    currentPosition= position;

    LatLng latLatPosition = LatLng(position.latitude, position.longitude);
    //Camera move
    CameraPosition cameraPosition = new CameraPosition(target: latLatPosition, zoom: 16);
    newGoogleMapController.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
    //newGoogleMapController.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));

    String address = await AssistantMethods.searchCoordinateAddress(position, context);
    
  }

'''

request.dart 文件

'''
    import 'dart:convert';
    import 'dart:html';
    
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/foundation.dart';
    import 'package:geolocator/geolocator.dart';
    import 'package:http/http.dart' as http;
    import 'package:rider_app/configMaps.dart';
    import 'package:rider_app/lib/AllScreens/mainscreen.dart';
    
    class RequestAssistant
    {
      static Future<dynamic> getRequest(url) async
      {
        
        
        
      var url = Uri.parse("https://maps.googleapis.com/maps/api/geocode/json?latlng=${position.latitude},${position.longitude}&key=$mapKey");
    
    
        http.Response response = await http.get(url);
    
        try
        {
          if(response.statusCode==200)
          {
            String jSonData = response.body;
            var decodeData = jsonDecode(jSonData);
            return decodeData;
          }
          else
          {
            return "failed";
          }
        }
        catch(exp)
        {
          return "failed";
        }
      }
    }'''

1 个答案:

答案 0 :(得分:0)

可以传入getRequest方法生命

getRequest(Position position) async {...}