getx模型中的类没有在主页中定义

问题描述 投票:0回答:1

这是需要存在

lowtohigh
功能和
hightolow
功能的控制器。

在主页上有

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:get/get.dart';

class Filteration extends GetxController {
  RxBool show = false.obs;

  Stream<QuerySnapshot> Detabase =
      FirebaseFirestore.instance.collection('Detabase').snapshots();
  Stream<QuerySnapshot> filterDatabase(int minPrice, int maxPrice) {
    return FirebaseFirestore.instance
        .collection('Detabase')
        .where('expectedrent', isGreaterThanOrEqualTo: minPrice)
        .where('expectedrent', isLessThanOrEqualTo: maxPrice)
        .snapshots();
    // ignore: dead_code
    Stream<QuerySnapshot>? increment() {
      if (show == false) {
        return FirebaseFirestore.instance.collection('Detabase').snapshots();
      } else {
        return Detabase;
      }
    }

    LowtoHigh() async {
      if (show == true) {
        Detabase = filterDatabase(1000, 100);
      }
    }

    HightoLow() {
      Detabase = filterDatabase(100, 1000);
    }

    handerdtofour() {
      Detabase = filterDatabase(100, 100);
    }
  }
}

这是主页,我想将这些功能中的任何一个放在这个主页中,主要是任何按钮的onPressed。

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:realstate/home%20page/addingpage.dart';
import 'package:realstate/home%20page/inkwall.dart';

import '../account page/account page.dart';
import 'filter_model.dart';

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  Stream<QuerySnapshot>? changer;
  double padding = 10;
  final Filteration c = Get.find();
  // bool show = false;

  // Stream<QuerySnapshot>? Detabase =
  //     FirebaseFirestore.instance.collection('Detabase').snapshots();
  // Stream<QuerySnapshot> filterDatabase(int minPrice, int maxPrice) {
  //   return FirebaseFirestore.instance
  //       .collection('Detabase')
  //       .where('expectedrent', isGreaterThanOrEqualTo: minPrice)
  //       .where('expectedrent', isLessThanOrEqualTo: maxPrice)
  //       .snapshots();
  // }

  // Stream<QuerySnapshot>? increment() {
  //   if (show == false) {
  //     return FirebaseFirestore.instance.collection('Detabase').snapshots();
  //   } else {
  //     return Detabase;
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          backgroundColor: Color.fromARGB(117, 53, 52, 52),
          appBar: AppBar(
            centerTitle: true,
            title: const Text(
              "Home",
              style: TextStyle(
                  color: Colors.black,
                  fontFamily: 'Montserrat',
                  fontWeight: FontWeight.bold,
                  fontSize: 32),
            ),
            leading: IconButton(
              icon: Icon(
                Icons.add_business_sharp,
                color: Colors.black,
                size: 26,
              ),
              onPressed: () {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => Addingpage()));
              },
            ),
            backgroundColor: Colors.white,
            actions: [
              IconButton(
                onPressed: () {
                  Navigator.of(context).push(
                      MaterialPageRoute(builder: (context) => AccountPage()));
                },
                icon: const Icon(
                  Icons.account_circle,
                ),
                color: Colors.black,
                highlightColor: Colors.transparent,
                focusColor: Colors.transparent,
                hoverColor: Colors.transparent,
                splashColor: Colors.transparent,
              )
            ],
          ),
          body: Padding(
            padding: const EdgeInsets.only(right: 21, left: 20),
            child: ThePoster(),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              showModalBottomSheet(
                context: context,
                builder: (context) {
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(
                              top: 25,
                              bottom: padding,
                              left: padding,
                              right: padding,
                            ),
                            child: SizedBox(
                              height: MediaQuery.of(context).size.height * 0.09,
                              width: MediaQuery.of(context).size.width * 0.20,
                              child: OutlinedButton(
                                style: ButtonStyle(),
                                onPressed: () {},
                                child: Text("test"),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(
                              top: 25,
                              bottom: padding,
                              left: padding,
                              right: padding,
                            ),
                            child: SizedBox(
                              height: MediaQuery.of(context).size.height * 0.09,
                              width: MediaQuery.of(context).size.width * 0.20,
                              child: OutlinedButton(
                                style: ButtonStyle(),
                                onPressed: () {},
                                child: Text("test"),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(
                              top: 25,
                              bottom: padding,
                              left: padding,
                              right: padding,
                            ),
                            child: SizedBox(
                              height: MediaQuery.of(context).size.height * 0.09,
                              width: MediaQuery.of(context).size.width * 0.20,
                              child: OutlinedButton(
                                style: ButtonStyle(),
                                onPressed: () {},
                                child: Text("test"),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(
                              top: 25,
                              bottom: padding,
                              left: padding,
                              right: padding,
                            ),
                            child: SizedBox(
                              height: MediaQuery.of(context).size.height * 0.09,
                              width: MediaQuery.of(context).size.width * 0.20,
                              child: OutlinedButton(
                                style: ButtonStyle(),
                                onPressed: () {
                                  
                                },
                                child: Text("test"),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  );
                },
              );
            },
          ),
        ));
  }
}

有谁知道这个答案吗?

请告诉我你的解决方案

flutter firebase google-cloud-firestore
1个回答
0
投票

我的解决方案(并不完美,但可能有效):

在您的 GetXController 中:

class FilterationController extends GetxController {
  
  bool _show = false;
  Stream<QuerySnapshot> _db =
      FirebaseFirestore.instance.collection('Database').snapshots();
  
  Stream<QuerySnapshot> db => _db;
  bool show => _show;
  
  
  setShow(bool value){
    _show = value;
    update(); // like notifyListeners or setState
  }
  
  setDb(Stream<QuerySnapshot> value){
    _db = value;
    update();
  }
  
Stream<QuerySnapshot> _filterDatabase(int minPrice, int maxPrice) {
     if(_show) {
    _db = FirebaseFirestore.instance
        .collection('Database')
        .where('expectedrent', isGreaterThanOrEqualTo: minPrice)
        .where('expectedrent', isLessThanOrEqualTo: maxPrice)
        .snapshots();
     } else {
       _db = FirebaseFirestore.instance.collection('Database').snapshots();
     }
    update();
}
  
// What is this for ? 
// Stream<QuerySnapshot> increment() {
//  if (!_show) {
//         return FirebaseFirestore.instance.collection('Database').snapshots();
//       } else {
//         return _db;
//       }
//     }
 

    lowtoHigh(){
      if (_show) {
        _db = _filterDatabase(1000, 100);
         update();
      }
    }

    hightoLow() {
       _db = _filterDatabase(100, 1000);
       update();
    }

    handerdtofour() {
      _db = _filterDatabase(100, 100);
      update();
    }
  }

在您的主页中:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:realstate/home%20page/addingpage.dart';
import 'package:realstate/home%20page/inkwall.dart';

import '../account page/account page.dart';
import 'filter_model.dart';

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  double padding = 10;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FilterationController>((controller)=> Scaffold(
          backgroundColor: Color.fromARGB(117, 53, 52, 52),
          appBar: AppBar(
            centerTitle: true,
            title: const Text(
              "Home",
              style: TextStyle(
                  color: Colors.black,
                  fontFamily: 'Montserrat',
                  fontWeight: FontWeight.bold,
                  fontSize: 32),
            ),
            leading: IconButton(
              icon: Icon(
                Icons.add_business_sharp,
                color: Colors.black,
                size: 26,
              ),
              onPressed: () {
                //******Call function like this
                controller.handerdtofour();
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => Addingpage()));
              },
            ),
            backgroundColor: Colors.white,
            actions: [
              IconButton(
                onPressed: () {
                  Navigator.of(context).push(
                      MaterialPageRoute(builder: (context) => AccountPage()));
                },
                icon: const Icon(
                  Icons.account_circle,
                ),
                color: Colors.black,
                highlightColor: Colors.transparent,
                focusColor: Colors.transparent,
                hoverColor: Colors.transparent,
                splashColor: Colors.transparent,
              )
            ],
          ),
          body: Padding(
            padding: const EdgeInsets.only(right: 21, left: 20),
            //********Example: Call db here*******, then manage snapshot states: eg. snapshot.hasData...
            child: StreamBuilder(stream:controller.db, (ctx,snapshot)=> ThePoster()),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              showModalBottomSheet(
                context: context,
                builder: (context) {
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(
                              top: 25,
                              bottom: padding,
                              left: padding,
                              right: padding,
                            ),
                            child: SizedBox(
                              height: MediaQuery.of(context).size.height * 0.09,
                              width: MediaQuery.of(context).size.width * 0.20,
                              child: OutlinedButton(
                                style: ButtonStyle(),
                                onPressed: () {},
                                child: Text("test"),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(
                              top: 25,
                              bottom: padding,
                              left: padding,
                              right: padding,
                            ),
                            child: SizedBox(
                              height: MediaQuery.of(context).size.height * 0.09,
                              width: MediaQuery.of(context).size.width * 0.20,
                              child: OutlinedButton(
                                style: ButtonStyle(),
                                onPressed: () {},
                                child: Text("test"),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(
                              top: 25,
                              bottom: padding,
                              left: padding,
                              right: padding,
                            ),
                            child: SizedBox(
                              height: MediaQuery.of(context).size.height * 0.09,
                              width: MediaQuery.of(context).size.width * 0.20,
                              child: OutlinedButton(
                                style: ButtonStyle(),
                                onPressed: () {},
                                child: Text("test"),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(
                              top: 25,
                              bottom: padding,
                              left: padding,
                              right: padding,
                            ),
                            child: SizedBox(
                              height: MediaQuery.of(context).size.height * 0.09,
                              width: MediaQuery.of(context).size.width * 0.20,
                              child: OutlinedButton(
                                style: ButtonStyle(),
                                onPressed: () {
                                  
                                },
                                child: Text("test"),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ));
                },
              );
            },
          ),
        );
  }
}

创建此文件:

class GlobalBindings implements Bindings {
  @override
  void dependencies() {
    Get.lazyPut<FilterationController>(() => FilterationController(),fenix: true);
  }
}

在main.dart中添加:

void main() async {
  .....

  GlobalBindings().dependencies();
  runApp(MyApp());
  .....
}

将您的主页附加到 main.dart:

 ......
MaterialApp(
 debugShowCheckedModeBanner: false,
 home: Home(),
)
.......
© www.soinside.com 2019 - 2024. All rights reserved.