当前位置:网站首页>[chapter 67 of the flutter problem series] the solution to the problem that the get plug-in cannot jump to the route twice in the dialog pop-up window in flutter

[chapter 67 of the flutter problem series] the solution to the problem that the get plug-in cannot jump to the route twice in the dialog pop-up window in flutter

2022-06-13 04:26:00 Allen Su

This is a 【Flutter Question Series No 67 piece 】, If it works , Welcome to the column .

For blog posts Flutter SDK:2.2.3,Dart SDK:2.13.4,get: 4.3.8.

One : Problem description

A problem discovered by chance before , It is hereby recorded that .

When it was put on the shelves of Xiaomi store , It is reported that there should be a pop-up window of privacy agreement when the user enters the application for the first time , Then I added one , Click on 《 User agreement 》 Can also jump to the corresponding Web page , I thought it would be a success , As shown in the figure below

I happened to be testing , When I first popped up in the pop-up window , Clicking the link for the first time can jump normally , sign out Web There is no response when the page is clicked again , Why is that ?

Breakpoint debugging , Found the root of the problem .

Two : Solution

Set up Get.toNamed() Properties of method preventDuplicates by false that will do , As shown in the following code

  ///  Jump to 《 User agreement 》 Webpage 
  void _toAgreementPage() {
    
    Get.toNamed(
      AppRoutes.WEB,
      arguments: WebModel(title: "《 User agreement 》", url: agreementUrl, isLocal: false),
      preventDuplicates: false, // “ Prevent duplication ” Set to  false
    );
  }

Then the problem is solved , This is not only for Dialog, Also suitable for Snackbar、BottomSheet Methods such as .

3、 ... and : Source code analysis 《 How to solve Dialog Pop up the problem of the second jump route exception 》

Some students like to break the casserole and ask the truth , Want to know that the problem is solved , But why ? Let's start with source code analysis .

First, let's take a look at... Through breakpoint debugging Get.toNamed() Method source code , As shown in the figure below

 Insert picture description here

It can be seen from the picture that ,toNamed() The comment above the method means

  • By default ,GetX It will prevent you from jumping to the route you have entered .
  • If you want to lift this restriction , Set properties preventDuplicates by false that will do .

From the location of the breakpoint , here preventDuplicates by true, Can enter this judgment again , explain page == currentRoute The conditions also hold , That is to say, the current route to jump is the route that has been entered .

Method returns null , So in Dialog Click the link again to jump Web There is no response when the page .

We just need to set preventDuplicates by false, The conditions are broken , Nature enters into the later judgment .

Four : Source code analysis 《 Why is there no such problem in the page Dialog In the pop-up window 》

One : When the route is stacked

Let's look at a class first GetObserver , This class inherits from NavigatorObserver class , The function is to monitor page Jump . As usual Toast And so on will inherit this class , GetObserver The source code of the class is shown in the following figure

 Insert picture description here

When I click 《 User agreement 》 Link to Web Page time , From the selected part of the red box , Only PageRoute The current route can only be modified if it is of type current Value of field , So it will be updated at this time .

That's why from Dialog There is no problem when the pop-up window enters the new page .

Two : When the route is out of the stack

As shown in the figure below

 Insert picture description here

When I click Web When the back button of the page exits the current page , From the selected part of the red box , Only PageRoute The current route can only be modified if it is of type current Value of field , And we are from Dialog Pop up window , The type of the previous page is GetDialogRoute, Does not satisfy the update current Conditions .

So although we did quit Web page , But this time current The value of has not been updated , still /web, That is to say, what the eyes see is a pop-up window , But the program thinks you're Web page , This leads to the problem that the same page cannot be skipped .

thus , About the Flutter Use in Get Plug in Dialog In the pop-up window, you cannot jump the route twice , That's it .

Last one get The class that determines the current route type , As shown in the figure below

 Insert picture description here

It can be seen from the picture that

  • Dialog The type is GetDialogRoute, Inherited from PopupRoute.
  • Snackbar The type is SnackRoute, Inherited from OverlayRoute.
  • BottomSheet The type is GetModalBottomSheetRoute, Inherited from PopupRoute.
  • Page The type is GetPageRoute, Inherited from PageRoute.

Has your problem been solved ? Feel free to leave a comment in the comments section .

A gift of rose , Fragrance in hand , If you think the article is good , I hope you can give me one button three times , thank .


Conclusion

Google Of Flutter More and more fire , end 2022 year 6 month 10 Japan GitHub Target star has reached 142K,Flutter Determination is a trend , So as a front-end Developer , There is no reason not to study as soon as possible .

No matter you are Flutter Novices are still getting started , You might as well pay attention to , In the future, I will Flutter Common components in ( Contains source code analysis 、 Usage and precautions of components ) As well as possible problems CSDN Blog , I hope I can learn at the same time , It can also help more people .
原网站

版权声明
本文为[Allen Su]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130420156163.html