Skip to content

Commit 6dfb4b4

Browse files
aranminoicmosuem
authored
fix: Updates the grpc-web example to avoid dart:html (#748)
* update: Migrate off legacy JS/HTML apis * update: use dart.library.js_interop in place of dart.library.html * update: dart format xhr_transport.dart and update dart sdk to v3.4.0 in workflows * update: use if instead of switch case in xhr_transport.dart * update: upgrade web package to v1.1.0 * refactor: use Uint8List for sending data over XHR rather than Int8List * refactor: eta-reduction of call to request.setRequestHeader * Convert grpc-web example to package:web --------- Co-authored-by: minoic <[email protected]> Co-authored-by: Moritz <[email protected]>
1 parent 5ba28e3 commit 6dfb4b4

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

example/grpc-web/lib/app.dart

+18-10
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
// limitations under the License.
1515

1616
import 'dart:async';
17-
// ignore: deprecated_member_use (#756)
18-
import 'dart:html';
19-
17+
import 'package:web/web.dart';
2018
import 'src/generated/echo.pbgrpc.dart';
2119

2220
class EchoApp {
@@ -57,13 +55,23 @@ class EchoApp {
5755
}
5856

5957
void _addMessage(String message, String cssClass) {
60-
final classes = cssClass.split(' ');
61-
querySelector('#first')!.after(DivElement()
62-
..classes.add('row')
63-
..append(Element.tag('h2')
64-
..append(SpanElement()
65-
..classes.add('label')
66-
..classes.addAll(classes)
58+
document.querySelector('#first')!.after(HTMLDivElement()
59+
..classList.add('row')
60+
..append(HTMLHeadingElement.h2()
61+
..append(HTMLSpanElement()
62+
..classList.add('label')
63+
..classList.addAll(cssClass)
6764
..text = message)));
6865
}
6966
}
67+
68+
// The documentation of DOMTokenList.add implies it can handle multiple classes,
69+
// but in Chrome at least it does not.
70+
extension AddAll on DOMTokenList {
71+
void addAll(String cssClass) {
72+
final classes = cssClass.split(' ');
73+
for (final c in classes) {
74+
add(c);
75+
}
76+
}
77+
}

example/grpc-web/pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies:
99
grpc:
1010
path: ../../
1111
protobuf: ^3.0.0
12+
web: ^1.1.0
1213

1314
dev_dependencies:
1415
build_runner: ^2.4.13

example/grpc-web/web/main.dart

+4-7
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,20 @@
1212
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
15-
16-
// ignore: deprecated_member_use (#756)
17-
import 'dart:html';
18-
1915
import 'package:grpc/grpc_web.dart';
2016
import 'package:grpc_web/app.dart';
2117
import 'package:grpc_web/src/generated/echo.pbgrpc.dart';
18+
import 'package:web/web.dart';
2219

2320
void main() {
2421
final channel = GrpcWebClientChannel.xhr(Uri.parse('http://localhost:8080'));
2522
final service = EchoServiceClient(channel);
2623
final app = EchoApp(service);
2724

28-
final button = querySelector('#send') as ButtonElement;
25+
final button = document.querySelector('#send') as HTMLButtonElement;
2926
button.onClick.listen((e) async {
30-
final msg = querySelector('#msg') as TextInputElement;
31-
final value = msg.value!.trim();
27+
final msg = document.querySelector('#msg') as HTMLInputElement;
28+
final value = msg.value.trim();
3229
msg.value = '';
3330

3431
if (value.isEmpty) return;

0 commit comments

Comments
 (0)