简体   繁体   English

颤振 - 在本地访问数据

[英]FLUTTER- ACCES DATA IN LOCAL

I have some problem that is very hard to explain... I am building an social media app.我有一些很难解释的问题......我正在构建一个社交媒体应用程序。 I wrote the login page with SharedPreferences and while I am doing this, I posted email and password to an url.我使用 SharedPreferences 编写了登录页面,在执行此操作时,我将 email 和密码发布到了 url。 The url I posted the datas contains some ID values.我发布的 url 包含一些 ID 值。 When I want to get this id values, flutter gives me error.当我想获得这个 id 值时,flutter 给了我错误。 I will add code and error to below.我将在下面添加代码和错误。 please help me...请帮我...

Login Page and post email&password:登录页面并发布电子邮件和密码:

PostValuesModel postEmailAndPasswordValuesFromJson(String str) =>
    PostValuesModel.fromJson(json.decode(str));

String postEmailAndPassword(PostValuesModel data) => json.encode(data.toJson());

class PostValuesModel {
  String email;
  String password;
  int servisLogin;
  String servisId;

  PostValuesModel({
    this.email,
    this.password,
    this.servisLogin,
    this.servisId,
  });

  factory PostValuesModel.fromJson(Map<String, dynamic> json) =>
      PostValuesModel(
        email: json["email"],
        password: json["password"],
        servisLogin: json["login"],
        servisId: json["servis_id"],
      );

  Map<String, dynamic> toJson() => {
        "email": email,
        "password": password,
        "login": servisLogin,
        "servis_id": servisId,
      };
}

Future<PostValuesModel> postEmailAndPasswordValues(
  String emailTitle,
  String passwordTitle,
) async {
  final String apiUrl = "https://rsad.com//dart.php";

  final response = await http.post(apiUrl, body: {
    "email": emailTitle,
    "password": passwordTitle,
  });

  if (response.statusCode == 200) {
    final String responseString = response.body;

    return postEmailAndPasswordValuesFromJson(responseString);
  } else {
    print('Error');
  }
}


class _LoginScreenState extends State<LoginScreen> {
  TextEditingController emailController = TextEditingController();
  TextEditingController passwordController = TextEditingController();

  bool isLoggedIn = false;
  String emailSession = '';

  PostValuesModel _email;
  PostValuesModel _password;
  PostValuesModel _servisLogin;
  PostValuesModel _servisId;

Widget _buildLoginButton() {
    Widget _buildChild() {
        return CupertinoButton(
          onPressed: () async {
            final String emailTitle = emailController.text.toString();
            final String passwordTitle = passwordController.text.toString();

            final PostValuesModel user = await postEmailAndPasswordValues(
              emailTitle,
              passwordTitle,
            );

            setState(() {
              _email = user;
              _password = user;
              _servisLogin = user;
              _servisId = user;
            });

            print(_servisId.servisId.toString());

            if (_servisLogin.servisLogin == 0) {
              Navigator.of(context);
            } else {
              Navigator.of(context).push(_routeServisLogin());
            }

            isLoggedIn ? logout() : loginUser();

            //Navigator.of(context).push(_routeLogin());
          },
          padding: EdgeInsets.all(15.0),
          color: Colors.red,
          child: Text(
            'Giriş Yap',
            style: TextStyle(
              color: Colors.white,
            ),
          ),
        );
    }

    return Container(
      padding: EdgeInsets.symmetric(vertical: 25.0),
      width: double.infinity,
      child: _buildChild(),
    );
  }
}

And I want to fetch values to here:我想在这里获取值:

BekliyorModel bekliyorModelFromJson(String str) =>
    BekliyorModel.fromJson(json.decode(str));

String bekliyorModelToJson(BekliyorModel data) => json.encode(data.toJson());

class BekliyorModel {
  String servis_id;
  String kategori;

  BekliyorModel({this.servis_id, this.kategori});

  factory BekliyorModel.fromJson(Map<String, dynamic> json) => BekliyorModel(
        servis_id: json["servis_id"],
        kategori: json["kategori"],
      );

  Map<String, dynamic> toJson() => {
        "servis_id": servis_id,
        "kategori": kategori,
      };
}

Future<BekliyorModel> postAndFetchBekliyorDatas(
  String servisIdTitle,
  String kategoriTitle,
) async {
  final String apiUrl =
      "https://www.rsad.com/dart.php";

  final response = await http.post(apiUrl, body: {
    "kategori": kategoriTitle,
    "servis_id": servisIdTitle,
  });

  if (response.statusCode == 200) {
    final String responseString = response.body;

    return bekliyorModelFromJson(responseString);
  } else {
    return null;
  }
}

class _ServisProfilState extends State<ServisProfil> {
  BekliyorModel _servisId;
  BekliyorModel _kategori;

 Widget _buildBekliyor() {
    return Container(
      height: 60,
      child: RaisedButton(
          highlightElevation: 0,
          splashColor: Colors.transparent,
          elevation: 0,
          color: Colors.white,
          child: Container(
            alignment: Alignment.centerLeft,
            child: Text(
              'Bekliyor/Sonuçlanan',
              style: TextStyle(fontSize: 18, color: Colors.black),
            ),
          ),
          onPressed: () async {
            PostValuesModel deneme;

            final String servisIdTitle = deneme.servisId.toString();
            final String kategoriTitle = 3.toString();

            final BekliyorModel user = await postAndFetchBekliyorDatas(
              kategoriTitle,
              servisIdTitle,
            );

            setState(() {
              _kategori = user;
              _servisId = user;
            });

            print(servisIdTitle.toString());

            // Navigator.of(context).push(
            //   CupertinoPageRoute(
            //     builder: (context) => Bekliyor(),
            //   ),
            // );
          }),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            width: 1,
            color: Colors.black12,
          ),
        ),
      ),
    );
  }
}

it gives me this error:它给了我这个错误:

[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The getter 'servisId' was called on null.
Receiver: null
Tried calling: servisId
#0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:51:5)
#1      _ServisProfilState._buildBekliyor.<anonymous closure> 
package:ekspar/Servis/service_profile.dart:116
#2      _InkResponseState._handleTap 
package:flutter/…/material/ink_well.dart:992
#3      _InkResponseState.build.<anonymous closure> 
package:flutter/…/material/ink_well.dart:1098
#4      GestureRecognizer.invokeCallback 
package:flutter/…/gestures/recognizer.dart:184
#5      TapGestureRecognizer.handleTapUp 
package:flutter/…/gestures/tap.dart:524
#6      BaseTapGestureRecognizer._checkUp 
package:flutter/…/gestures/tap.dart:284
#7      BaseTapGestureRecognizer.acceptGesture 
package:flutter/…/gestures/tap.dart:256
#8      GestureArenaManager.sweep 
package:flutter/…/gestures/arena.dart:158
#9      GestureBinding.handleEvent (<…>

I think, here needs to edit:我认为,这里需要编辑:

   final String servisIdTitle = deneme.servisId.toString();

please help me...请帮我...

It is because of this part of code..就是因为这部分代码..

PostValuesModel deneme;
final String servisIdTitle = deneme.servisId.toString();

You just declared the variable deneme in the first line without assigning a value to it, and then in the second line, you are trying to call the getter servisId on it, while your variable deneme is still null, just like the error says.您只是在第一行中声明了变量deneme而没有为其分配值,然后在第二行中,您尝试在其上调用 getter servisId ,而您的变量deneme仍然是 null,就像错误所说的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM