@@ -63,6 +63,7 @@ class _SearchPageState extends State<SearchPage> {
6363 });
6464 List <CityData > cityData = [];
6565 var result = await WeatherApi ().searchCity (keywords);
66+ weatherPrint ("search result $result " );
6667 if (keywords != _keyWords) {
6768 return ;
6869 }
@@ -93,6 +94,9 @@ class _SearchPageState extends State<SearchPage> {
9394 });
9495 }
9596 });
97+ } else if (result.info == "INVALID_REQUEST" ) {
98+ weatherPrint ("高德 API 调用次数上限" );
99+ ToastUtils .show ("由于高德 API 调用次数上限,无法搜索,请见谅" , context, duration: 5 , gravity: 2 );
96100 }
97101 }
98102 if (cityData.isEmpty) {
@@ -130,19 +134,37 @@ class _SearchPageState extends State<SearchPage> {
130134 }
131135 }
132136
137+ CityModel _buildDefault () {
138+ CityModel cityModel = CityModel (
139+ latitude: 39.904989 ,
140+ longitude: 116.405285 ,
141+ country: "中国" ,
142+ province: "北京市" ,
143+ district: "东城区" ,
144+ displayedName: "北京市" ,
145+ );
146+ return cityModel;
147+ }
148+
133149 Future <void > onItemClick (CityData cityData, bool fromList) async {
134150 showAppDialog ();
135151 var result = await WeatherApi ().reGeo (cityData.center);
152+ weatherPrint ("item click regeo result: $result " );
136153 if (result == null ) {
137154 Navigator .of (context).pop ();
138155 ToastUtils .show ("添加失败请重试" , context);
139156 return ;
140157 }
141158 CityModel cityModel = await parseCityModel (result, cityData);
142- if (fromList) {
143- cityModel.displayedName = "${WeatherUtil .getCityName (cityModel )}" ;
159+ if (cityModel == null ) {
160+ ToastUtils .show ("高德 API 调用上限, 默认添加北京,请见谅" , context, duration: 5 );
161+ cityModel = _buildDefault ();
144162 } else {
145- cityModel.displayedName = cityData.name;
163+ if (fromList) {
164+ cityModel.displayedName = "${WeatherUtil .getCityName (cityModel )}" ;
165+ } else {
166+ cityModel.displayedName = cityData.name;
167+ }
146168 }
147169 BlocProvider .of <CityBloc >(context).add (InsertCityData (cityModel));
148170 await Future .delayed (Duration (milliseconds: 20 ));
@@ -171,6 +193,8 @@ class _SearchPageState extends State<SearchPage> {
171193 addressComponent["city" ] is String ) {
172194 cityModel.city = addressComponent["city" ];
173195 }
196+ } else {
197+ return null ;
174198 }
175199 return cityModel;
176200 }
0 commit comments